shell script to start multiple Java programs from a directory at boot
- by zcourts
I'm not sure if this is the best approach to this, It's my first time doing all of this (including writing shell scripts).
OS:
Centos
My problem:
I want to start multiple shell scripts at boot.
One of the shell scripts is to start my own services and 3 others are for third party services.
The shell script to start my own services will be looking for jar files.
I currently have two services (will change), written in Java.
All services are named under convention prefix-service-servicename
What I've done:
I created the following directory structure
/home/username/scripts
init.sh
boot/
boot/startthirdprtyservice1.sh
boot/startthirdprtyservice2.sh
boot/startthirdprtyservice3.sh
boot/startmyservices.sh
/home/username/services
prefix-lib-libraryname.jar
prefix-lib-libraryname.jar
prefix-service-servicename.jar
prefix-service-servicename.jar
prefix-service-servicename.jar
In init.sh I have the following:
#!/bin/sh
#This scripts run all executable scripts in the boot directory at boot
#done by adding this script to the file /etc/rc.d/rc.local
#nohup
#run-parts /home/username/scripts/boot/*
#for each file in the boot dir...
# ignore the HUP (hangup) signal
for s in ./boot/*;do
if [ -x $s ]; then
echo "Starting $s"
nohup $s &
fi
done
echo "Done starting bootup scripts "
echo "\n"
In the script boot/startmyservices.sh I have
#!/bin/sh
fnmatch () { case "$2" in $1) return 0 ;; esac ; return 1 ; }
##sub strin to match for
SUBSTRING="prefix-service"
for s in /home/username/services/*;do
if [ -x $s ]; then
#match service in the filename , i.e. only services are started
if fnmatch "$SUBSTRING" "$s" ; then
echo "Starting $s "
nohup $s &
fi
fi
done
echo "Done starting Services"
echo "\n"
Finally:
Usually you can stick a program in /etc/rc.d/rc.local
for it to be run at boot but I don't think this works in this case, or rather I don't know what to put in there
I've just learnt how to do this by reading up a bit so I'm not sure its particularly the best thing to do so any advice is appreciated.
When I run init.sh nohup.out contains
Starting the thirdparty daemon...
thirdparty started...
....
but nothing from myservices.sh and my Java services aren't running
I'm not sure where to start debugging or what could be going wrong.
Edit
Found some issues and got it to work, used -x instead of -n to check if the string is none zero, needed the sub string check to also be if [[ $s = $SUBSTRING ]] ; then and this last one was just stupid, missing java -jar in front of $s
Still unsure of how to get init.sh to run at boot though