Since system administrators are inherently lazy people (I know this because I am one), we don't like to have to rummage around a system trying to find out what directory a startup script is in, nor do we want to have to write separate startup and shutdown scripts for an application.
Given this, the people who designed SVR4 UNIX decided that there should be a common location for application startup scripts and that location should be "/etc/init.d" (/etc/rc.d/init.d on Linux).
Each script is capable of taking a single argument, which can be either
"start" or "stop".
Based on the argument passed, the script "does the right thing" to stop
or start an application.
Here is the procedure I followed when writing the startup script for the PKC webcrossing daemon
I then customized the script for webcrossing until it looked like:
--------------------
#!/bin/sh
# this script will start/stop the WebCrossing Server for pkc
INSTALLDIR=/opt/webx
RUNAS=pkcadm
export RUNAS
case $1 in
'start')
# recall the script as the non-priviliged user
su $RUNAS -c "cd $INSTALLDIR;./webx-run -daemon -server"
;;
'stop')
cd $INSTALLDIR
echo "stop the Web Crossing server"
kill `cat webx.lock`
;;
*)
echo "Usage: $0 start|stop" >&2
exit 1
;;
esac
exit 0
--------------------
Notice that since the webcrossing server is capable of running as a non-root user, we su to another user before starting the daemon.
Now, we need this shell script to be executed each time the system boots. We can choose to have the "init" program run this command in run-level 2 or run-level 3. I arbitrarily chose run-level 3, which means that I need to link the program from /etc/init.d into /etc/rc3.d.
Here is the command that I used:
# ln -s /etc/init.d/pkcwebcrossing /etc/rc3.d/S20pkcwebcrossing
I also want the system to automatically shutdown webcrossing when the administrator gracefully shuts down the system (using shutdown or init NOT reboot or halt).
So, I also link the startup/shutdown script into the /etc/rc0.d directory:
# ln -s /etc/init.d/pkcwebcrossing /etc/rc0.d/K31pkcwebcrossing
I noticed that my shutdown scripts were NOT being executed on Linux at system shutdown. After going over the linux script /etc/rc.d/rc, I found that linux (in its infinite wisdom) decided to add an additional "feature" to the start/stop scripts.
Linux will only run your stop script if it can determine that the
"subsystem" is running.
In order to determine this, it looks for a file in /var/lock/subsys.
Change the template above to the following:
--------------------
#!/bin/sh
# AWB - added this because linux, or at least mandrake won't shutdown
# the "subsystem" without it!
subsys=`basename $0`
subsys=`echo $subsys |sed -e "s/^[SK]..//"`
[ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
case $1 in
'start')
# put startup command(s) here
touch /var/lock/subsys/$subsys
;;
'stop')
# put shutdown command(s) here
rm -f /var/lock/subsys/$subsys
;;
*)
echo "Usage: $0 start|stop" >&2
exit 1
;;
esac
exit 0
--------------------