Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute a simple command just before the computer shuts down (timing is not essential).

For startup I can use /etc/rc.local - is there something similar for shutdown?

Note that I would still like to use the integrated shutdown button from menu; i.e. I don't want to use a custom script everytime I shutdown via terminal - it needs to be automatical.

share|improve this question
up vote 24 down vote accepted

Linux Mint is based on Ubuntu, so I'm guesing the runlevel system is probably the same. On Ubuntu, scripts for the different runlevels are executed according to their presence in the /etc/rc[0-6].d directories. Runlevel 0 corresponds to shutdown, and 6 to reboot.

Typically the script itself is stored in /etc/init.d, and then symlinks are placed in the directories corresponding to the runlevels you require.

So in your case, write your script, store it in /etc/init.d/, then create a symlink in each of /etc/rc0.d and /etc/rc6.d (if you want both) pointing to your script.

The scripts in each runlevel directory will be executed in asciibetical order, so if the order within the runlevel matters to you, choose the name of your symlink accordingly.

share|improve this answer
I just took a look at the skeleton file in init.d (kubuntu precise) and got a bit intimidated. I just want to add a couple of commands that echo the date of shutdown into a file under /var/log. Can I just do that or do I have to deal with some of the other things in the skeleton file? – Joe Sep 28 '12 at 20:02
@Joe - You don't need most of that stuff unless you want your script to be controllable as a standard service. You won't break anything with a simple echo to your own log file, so try it and see. – ire_and_curses Sep 28 '12 at 23:34
Here is a simple sample init.d script. Also, it's easier and more robust to make the symlinks using sudo update-rc.d /etc/init.d/myservice defaults – RolfBly Dec 9 '14 at 17:12

You need to use rc.shutdown the shutdown script for the Linux Kernel. Quoting the OpenBSD man page:

When the system is shut down using the reboot(8) or halt(8) commands, or when init(8) is signalled to do so, or when a keyboard-requested halt is issued (if the architecture supports it), rc(8) is invoked with the argument ``shutdown''.

So, you simply open your rc.shutdown and add whatever shell commands you want to execute to it.

UPDATE: Since Linux Mint is based on Ubuntu, which has a different startup/shutdown procedure, here is the procedure relevant to you:

Write the shell script you wish to execute and copy it to the relevant directory in your /etc/rc*.d/. The * corresponds to the runlevel at which you want the script to execute.
Ubuntu follows the Debian runlevel numbering, so you have runlevel 0 for halt and runlevel 6 for reboot.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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