RHEL6 Can't make my script run on shutdown and reboot

10,520

Solution 1

When you configure a custom init script in RH EL 6, named for example "service_name", you have to care to create, during its start phase, the lock file /var/lock/subsys/service_name otherwise the script will not be executed during shutdown of the system (init 0 or init 6). At the same time you should setup "rm -f" of the lock file during its stop phase.

See https://access.redhat.com/solutions/701383

Solution 2

If you check what is in /etc/rc0.d and /etc/rc6.d, you will see that before you get to your service, the system will have run first a killall (priority 00), and then a halt (priority 01).

Although your service were scheduled to run next, the computer did already halt.

lrwxrwxrwx 1 root root 17 Sep 28  2012 S00killall -> ../init.d/killall
lrwxrwxrwx 1 root root 14 Sep 28  2012 S01halt -> ../init.d/halt
lrwxrwxrwx 1 root root 14 Jul  8 21:16 S10test -> ../init.d/test

What I'd do if I were you, is to enable the script in runlevels you typically use, say 345 and then swap start with stop, as all daemons which is chkconfig:ed to be running in your current runlevel will be called when halting and rebooting with the stop arg, before it gets to the services which it should start.

If you want your script to be run at the very last, before killing everything and then halting your computer, check what is the highest (mine's K92iptables, so for me that would be >92 for stop priority).

So chkconfig --del baseRhel64 first, then alter the script so that chkconfig line reads # chkconfig: 345 10 93

Your script would then look something like this, with start and stop function names swapped:

#!/bin/sh

#chkconfig --list
# chkconfig: 345 10 93

stop(){
    echo "`basename $0` stop"
    touch /root/installscripts/test1
}

start(){
    echo "`basename $0` start"
    touch /root/installscripts/test2
    touch /root/installscripts/"`basename $0`"
}

case "$1" in
    start) start;;
    stop) stop;;
    *);;
    echo $"Usage: $0 {start|stop}"
    RETCAL=1
esac
exit 0
Share:
10,520
Gidi Kern
Author by

Gidi Kern

Updated on September 18, 2022

Comments

  • Gidi Kern
    Gidi Kern almost 2 years

    I'm trying to execute a script in each shutdown and reboot but the script never runs.

    1. I created the script baseRhel64 and save it to /etc/rc.d/init.d
    2. I did chkconfig --add baseRhel64
    3. I included in the script

      #chkconfig --list
      # chkconfig: 06 10 10
      
    4. I verified that the S10 script where created under /etc/rc0.d/S10baseRhel64 and /etc/rc6.d/S10baseRhel64

    The following is my script:

    #!/bin/sh
    
    #chkconfig --list
    # chkconfig: 06 10 10
    
    start(){
        echo "`basename $0` start"
        touch /root/installscripts/test1
    }
    
    stop(){
        echo "`basename $0` stop"
        touch /root/installscripts/test2
        touch /root/installscripts/"`basename $0`"
    }
    
    case "$1" in
        start) start;;
        stop) stop;;
        *) 
        echo $"Usage: $0 {start|stop}"
        RETCAL=1
    esac
    exit 0
    
    • user1845506
      user1845506 about 10 years
      I think the init script needs to follow a certain header format, see an example here: fedoraproject.org/wiki/Packaging:SysVInitScript?rd=Packaging‌​/…
    • Jakob Bennemann
      Jakob Bennemann about 10 years
      It may also be helpful to note that you should still have a ;; at the end of your default (*)) case.
    • Gidi Kern
      Gidi Kern about 10 years
    • Petter H
      Petter H almost 10 years
      Did it show up when you ran chkconfig? Does the runlevels stated correspond with what you're running?? Is it true you want it only during reboot 6 and 0halt? Add some debugging to it ie redirect stdout and stderr to see if it gets called and what args are in $1