Debian: How to split rc.local in rc.local.stop and rc.local.start

root's picture

Debian is great but sometimes basic simple things are missing so we have to step in.
For example, Debian is coming by default with /etc/rc.local where you can put your custom commands which are automatically executed on startup. But what about custom stop script to be executed on shutdown or reboot? This is missing.
No problem! All you can do is follow the instructions below.

1. Edit or replace /etc/init.d/rc.local with this one:

root@pluto:~# cat /etc/init.d/rc.local 
#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
	if [ -x /etc/rc.local.start ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local.start)"
		/etc/rc.local.start
		ES=$?
		[ "$VERBOSE" != no ] && log_end_msg $ES
		return $ES
	fi
}
do_stop() {
    if [ -x /etc/rc.local.stop ]; then
            [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local.stop)"
        /etc/rc.local.stop
        ES=$?
        [ "$VERBOSE" != no ] && log_end_msg $ES
        return $ES
    fi
}


case "$1" in
    start)
	do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        do_stop
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

2. Copy/move /etc/rc.local into 2 files: /etc/rc.local.stop and /etc/rc.local.start.

root@pluto:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local.stop
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

root@pluto:~# cp /etc/rc.local /etc/rc.local.start
root@pluto:~# mv /etc/rc.local /etc/rc.local.stop
root@pluto:~# chmod +x /etc/rc.local.*

3. Update startup sequence for /etc/init.d/rc.local:

root@pluto:~# update-rc.d -f rc.local remove
update-rc.d: using dependency based boot sequencing

root@pluto:~# update-rc.d rc.local defaults
update-rc.d: using dependency based boot sequencing

root@pluto:~# ls -l /etc/rc?.d/*local*
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc0.d/K01rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc1.d/K01rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc2.d/S18rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc3.d/S18rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc4.d/S18rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc5.d/S18rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 Dec 11 15:27 /etc/rc6.d/K01rc.local -> ../init.d/rc.local

DONE! Now you have two distinct /etc/rc.local* scripts:
- /etc/rc.local.start <- put your startup scripts/commands here;
- /etc/rc.local.stop <- put your shutdown scripts/commands here;

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root