The other day I had an irritating issue with an init script on RedHat. I had enabled the script using chkconfig command. The service would start up just fine, but when the server was bounced, the script was skipped and stop operation was not performed.

I had checked all the symlinks in rc.* directories and everything seemed fine. Despite that script’s stop was not called during reboot/shutdown.

So I took a look at /etc/rc.d/rc script since it is responsible for handling init scripts when runlevel change occurs. And I came across following section:

# 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

The code checked for presence of /var/lock/subsys/ lockfile and if that file was present _stop_ operation was performed. Then I looked at my init script and there was nothing that would create the lock file when the _start_ operation was performed. So, I added the snippet below towards end of the _start_ section of my init script.

        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/crond;
        return $RETVAL

That had fixed the issue.