I have created a custom system 5 INIT start-stop script managed by chkconfig, but it does not stop the service, what may be wrong?

Solution Unverified - Updated -

Environment

  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • initscripts

Issue

Created a custom system 5 INIT start-stop script managed by chkconfig, but it does not stop the service, what may be wrong?

Resolution

There is a requirement that a start-stop script uses the /var/lock/subsys directory to place a lock file in that location when a service is started, and to remove the lock file when the service is shutdown.

It is also a requirement that the lockfile is named the same as the file name of the start-stop script itself. The reason for this is the way the /etc/rc.d/rc script 'kills' services as it enters a run level. The following example shows the section in the script file that checks for a lock file:

# 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

When the system enters a runlevel where it needs to stop services, /etc/rc.d/rc iterates over all the files in the relevant /etc/rcN.d directory with 'K' at the beginning. For example, when a system enters run level 6 (Shutdown) /etc/rc.d/rc will look in /etc/rc6.d/ directory for all files beginning with 'K':

# ll /etc/rc6.d/
total 412
lrwxrwxrwx 1 root root 20 Jul 15 09:04 K00xendomains -> ../init.d/xendomains
lrwxrwxrwx 1 root root 17 Jan 15  2008 K01dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx 1 root root 19 Mar 29 13:47 K01rgmanager -> ../init.d/rgmanager
lrwxrwxrwx 1 root root 15 Mar 15 15:02 K01rhnmd -> ../init.d/rhnmd
...,

The script /etc/rc.d/rc extracts the name of the start-stop script in the code and sets the subsys variable to the value it finds:

subsys=${i#/etc/rc$runlevel.d/K??}

It then looks for a file matching that name in the /var/lock/subsys directory:

[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue

If a file by that name is not found, then /etc/rc.d/rc will effectively stop processing and go to the next file in the runlevel. Thus you will see that a stop script is 'skipped' with no clear explanation why if the lock file is not created with the correct filename.

The solution is to make sure your start-stop script creates a lockfile of the correct name in /var/lock/subsys/ . Here is a simple example script that does this correctly:

#!/bin/sh
# myexample Start Stop imaginary myexample service
#
# chkconfig: 345 99 01 
# description: This script is a simple example of how to
# use the /var/lock/subsys lock files
# to ensure that the script is run when 
# entering a run level where the script 
# is required to STOP services

# NOTES Save this script in /etc/rc.d/init.d/ as file 
# name 'myexample', Run chkconfig --add myexample 
# this will add the required links in the runlevels 
# we need to start and stop in. 
# In this example we start in run levels
# 3, 4, and 5 with a priority of 99 (last) 
# and stop in all other run levels 
# with priority 1 (first)
#
# We use the logger command to send messages to syslog
# so we know that the start and stop sectiopns are ru!
n


RETVAL=0

case $1 in
'start')
 logger "We are starting the service called myexample"
 RETVAL=$?
 echo 
 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/myexample
 exit $RETVAL
 ;;
'stop')
 logger "We are stopping the myexample service"
 RETVAL=$?
 echo
 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/myexample
 exit $RETVAL
 ;;
*)
 echo "usage: $0 {start|stop}"
 exit
 ;;
esac
exit

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments