The script /etc/rc.local runs at boot time just before user login. But what about a system-wide shutdown script?

Some linux distributions include an /etc/rc.shutdown script that runs at system shutdown, after user logout but before any services or daemons are stopped. It's not difficult to add to ubuntu.

Copy the following text to a file and name it "rc.shutdown" I've included a few comments you can change or delete, and the 3 lines that end in "#optional" can be left out if you like. They just put a little message in the shutdown splash screen. (don't change the first line, "#!/bin/sh" ; it's required!)

#! /bin/sh
# Don't delete the above line or put anything before it.
# Calls /etc/rc.shutdown before any other shutdown scripts in
# rc0.d or rc6.d are run. Implements a shutdown analog to rc.local.
# Use a link number of K00 to ensure rc.shutdown is called before
# any services or daemons are stopped.
# NOTE: Starting runlevel 0 for shutdown or runlevel 6 for 
# reboot does NOT run the Snn... links with "start"
# Entering runlevel 0 or 6 causes ALL links to be run with the argument
# "stop" See /usr/share/doc/sysv-rc/README.runlevels

. /lib/lsb/init-functions #optional

case "$1" in 
    if [ -x /etc/rc.shutdown ]; then
      log_begin_msg "Running local shutdown scripts (/etc/rc.shutdown)" #optional
      log_end_msg $? #optional

    echo "Error: argument '$1' not supported" >&2
    exit 3

    # no op in case link is installed in a normal runlevel

    echo "Usage: $0 start|stop" >&2
    exit 3


Put the file in /etc/init.d with root ownership and execute permissions. From a shell prompt in the directory where you have saved the file, you can use the commands:

sudo cp rc.shutdown /etc/init.d
sudo chmod +x /etc/init.d/rc.shutdown

Delete or rename your original, non-root copy of rc.shutdown; we'll be using the same name for another, different file.

Copy the next file, also named rc.shutdown, to /etc/ with executable permission. This is the file where you can include any commands or scripts you want to run at shutdown. The comments in the file explain its use.

#! /bin/sh

# Commands or scripts called from this file will be run at shutdown, 
# before any services or processes are stopped by the normal
# shutdown scripts. By default it does nothing. Your commands or
# script should exit 0 for success or other value on error. 
# Delete the "exit 0" line from this file or make sure your commands 
# are placed before that line.

exit 0

sudo cp rc.shutdown /etc
sudo chmod +x /etc/rc.shutdown

I know, it's a little confusing using 2 different files with the same name, and easy enough to change if you like, but I've followed the same naming convention used for the /etc/rc.local and /etc/init.d/rc.local startup files.

The last thing necessary to actually have this script run at shutdown, is to make the proper links to /etc/init.d/rc.shutdown from the /etc/rc0.d (shutdown runlevel) and /etc/rc6.d (reboot runlevel) directories. The script must run before any system services are stopped, so name the link "K00rc.shutdown" (Those are zeros, not the letter 'O') Use these commands:

sudo ln -s /etc/init.d/rc.shutdown /etc/rc0.d/K00rc.shutdown
sudo ln -s /etc/init.d/rc.shutdown /etc/rc6.d/K00rc.shutdown

You can also use the update-rc.d command line utility to change init script links. (Try man update-rc.d for details) See modifying runlevels for more information.

*  *  *