Run a script at shutdown

Print

ubuntu-logo-smQ: I would like to run a script at system shutdown, how do I do that?

A: The best way to do so, is to add it to the /etc/init.d directory and link it to rc0 and rc6 (reboot and shutdown). Here's a sample script:

For the sake of this exercise, let's call it: shutdown_job

#! /bin/sh
### BEGIN INIT INFO
# Provides:          shutdown_job
# Required-Start:    
# Required-Stop:     sendsigs
# Default-Start:
# Default-Stop:      0 6
# Short-Description: description
# Description:
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/lsb/init-functions
do_stop () {
    #YOUR SHUTDOWN TASKS SHOULD BE HERE
    echo "shutting down.."
}
case "" in
  start)
    # No-op
    ;;
  restart|reload|force-reload)
    echo "Error: argument '' not supported" >&2
    exit 3
    ;;
  stop)
    do_stop
    ;;
  *)
    echo "Usage: {xtypo_code} #! /bin/sh
### BEGIN INIT INFO
# Provides:          shutdown_job
# Required-Start:    
# Required-Stop:     sendsigs
# Default-Start:
# Default-Stop:      0 6
# Short-Description: description
# Description:
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/lsb/init-functions
do_stop () {
    #YOUR SHUTDOWN TASKS SHOULD BE HERE
    echo "shutting down.."
}
case "$1" in
  start)
    # No-op
    ;;
  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
:{/xtypo_code} start|stop" >&2
    exit 3
    ;;
esac
:


Don't forgot to run "chmod a+x shutdown_job" to make it executable
Once the file is created, you can link using update-rc.d

update-rc.d shutdown_job start 19 0 6 .

 

Linux