Take the 2-minute tour ×
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.

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
    
What distro are you using? –  ire_and_curses Sep 24 '12 at 8:47
    
I am using Linux Mint. –  Primož Kralj Sep 24 '12 at 8:52

2 Answers 2

up vote 16 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
    
Thank you for your time. –  Primož Kralj Sep 24 '12 at 9:35
    
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
1  
@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

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
    
You seem to mix up file naming - it should be /etc/rc*.d! Other than that thank you too! –  Primož Kralj Sep 24 '12 at 9:36
1  
Right... My Bad.. I'm not exactly a Ubuntu guy.. Prefer using Arch, so know my way around that.. –  darnir Sep 24 '12 at 10:08

Your Answer

 
discard

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.