Shell Script – Execute a Command Before Shutdown in Linux Mint

linux-mintshell-scriptshutdown

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 every time I shutdown via terminal – it needs to be automatic.

Best Answer

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.

Related Question