Linux auto start/stop scripts
Posted by Vishal Gupta on May 17, 2010
Normally DBAs and SAs have a tendency to create the auto start/stop scripts like following.
# cat /etc/init.d/oracle case $1 in start) /opt/oracle/product/11.1.0/bin/dbstart; ;; stop) /opt/oracle/product/11.1.0/bin/dbshut; ;; esac # ln -s /etc/init.d/Oracle /etc/rc.d/rc0.d/K01oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc3.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc4.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc5.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc6.d/K01oracle
This works fine on non-Linux operating systems. But on Linux operating system, there is an requirement to for start/stop script to create/remove a /var/lock/subsys/ file, where is name of the file in /etc/init.d. This ensures that, when init is changing runlevel as part of server start and stop routine, a service which is already started does not start again. And a service which is already stopped does not stop again. If /var/lock/subsys/ is not created then inspite of K(ill) symbolic links being present in rc*.d folder for runlevel zero (halt) and 6 (reboot), stop script is not called. This means process are killed instead of being shutdown cleanly by your stop script.
On Linux Operating system, start/stop functions for a service init.d script should look like below. Important part is highlighted in red below
# chkconfig: 2345 99 01
# description: This script start/stop Oracle case $1 in start) /opt/oracle/product/11.1.0/bin/dbstart; touch /var/lock/subsys/Oracle; # where is same as name of file in /etc/init.d ;; stop) /opt/oracle/product/11.1.0/bin/dbshut; rm –f /var/lock/subsys/Oracle; ;; esac # ln -s /etc/init.d/Oracle /etc/rc.d/rc0.d/K01oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc3.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc4.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc5.d/S99oracle # ln -s /etc/init.d/Oracle /etc/rc.d/rc6.d/K01oracle
This applies to not only oracle but all start/stop created for init.d on Linux.
This requirement to create subsystem lock file in /var/lock/subsys can be verified by looking at /etc/rc script, which is called everytime there is runlevel change. See below the portion of /etc/rc script which is checking for presense of /var/lock/subsys/ file before calling Kill/stop scripts.
# First, run the KILL scripts. for i in /etc/rc$runlevel.d/K* ; do check_runlevel "$i" || continue # Check if the subsystem is already up. subsys=${i#/etc/rc$runlevel.d/K??} [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \ || continue # Bring the subsystem down. if LC_ALL=C egrep -q "^..*init.d/functions" $i ; then $i stop else action $"Stopping $subsys: " $i stop fi done
vinod said
Hello Vishal.
R u vishal worked in CMC(Mumbai) between 1999-2000
bye
Vishal Gupta said
Vinod,
No, i never worked in CMC