4

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
5
  • I think the init script needs to follow a certain header format, see an example here: fedoraproject.org/wiki/Packaging:SysVInitScript?rd=Packaging/…
    – fduff
    Jul 8 '14 at 8:23
  • It may also be helpful to note that you should still have a ;; at the end of your default (*)) case.
    – HalosGhost
    Jul 8 '14 at 8:42
  • I tried following fedoraproject.org/wiki/Packaging:SysVInitScript?rd=Packaging/… but still didn't manage.
    – Gidi Kern
    Jul 8 '14 at 13:28
  • service baseRhel64 start | stop | status all work fine but not during shutdown and restart
    – Gidi Kern
    Jul 8 '14 at 14:21
  • 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
    – Petter H
    Jul 8 '14 at 19:01
2

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

1

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
0

I have a fine solution for you a)A script like this

#!/bin/sh
#
# localshutdown
#
# chkconfig:   06 01 25
# description: localshutdown script
#              

### BEGIN INIT INFO
# Provides: 
# Required-Start: 
# Required-Stop: 
# Should-Start: 
# Should-Stop: 
# Default-Start: 0 6 
# Default-Stop:
# Short-Description: 
# Description:      
### END INIT INFO

# Source function library.

start() {
    echo "Your commands here"
    touch /root/funziona #use it for a test
}

stop() {
    echo "nothing" >/dev/null 2>&1
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        $1
        ;;
    stop)
        stop
        $1
        ;;
    restart)
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 2
esac
exit $?

Then

chkconfig --add /etc/init.d/yourscript.sh

then

chkconfig --level 06 yourscript.sh on
chkconfig --level 12345 yourscript.sh off

Tested and working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.