0

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

2 Answers 2

1

Personally I would use init.d scripts for all of the services instead of creating a shell script that launches another process.

So look into creating init.d script and then you can use chkconfig --add to add your app to startup.

Here is one example

1
  • Good answer but needs more detail. Also, you could create a simple script to monitor your services and send email when they go down, and put that script into a cron job. (note: requires enabling sendmail to accept mails from localhost)
    – djangofan
    Nov 11, 2011 at 17:13
0

Here is something I found on the net:

cp <script-file> /etc/init.d
ln -s /etc/init.d/<scriptfile> /etc/rc.d/rc5.d/S50<scriptfile>
ln -s /etc/init.d/<scriptfile> /etc/rc.d/rc5.d/K50<scriptfile>

The S50 is to tell the system to start the script when it boots up, the K50 is to tell the system to shut down cleanly when you do a shut down. The number representates in which order the script should start/shut down in.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .