(I've searched for answers for this, but all seem to be for older versions of Ubuntu.)
Is there a similar crontab trigger such as "@reboot" for shutdown scripts, and if not, why not?
(I've searched for answers for this, but all seem to be for older versions of Ubuntu.)
Is there a similar crontab trigger such as "@reboot" for shutdown scripts, and if not, why not?
No, because @reboot is
Run once, at startup.
but running a script at shutdown that way makes no sense because your system is going down and the system would 'kill' your script.
When you shutdown
your system, it will first send the SIGTERM and wait some time before killing any program, so you can react to that. Here is how.
I don't understand why you would want to run a script from cron when the system is shutting down, perhaps I am missing something. Instead of running a script at shutdown, why not just run a script which does what you need it to do before calling the shutdown command. For example, call the script preshutdown.sh:
Run command 1 and output results to a log file.
Run command 2 and append results to the log file ....etc
Shutdown when finished with sudo shutdown -h now
When you next power up the system you can view the log to see the results of the commands which ran in the script.
Edit: To answer the OP's question exactly, there is no crontab trigger for shutdown. If you want to run scripts when pressing the "Shutdown" button in the GUI, or by running the shutdown command in a terminal, put them in /etc/rc6.d, where they will be executed in alphabetical order. Their names must start with K99 and they must be executable.
@reboot
- if you need to run a script at startup, you might as well wait until your system boots and then run it manually. So why do we have @reboot
? The answer is simple: convenience. Instead of running the script manually every time, just let it run automatically - that's what computers are for, automating things. Same applies for shutdown - if someone needs to run some script everytime before shutdown, why run it always manually if it can run automatically?
– raj
Feb 17 '21 at 20:43
/etc/rc*.d
scripts can be used for that. Shutting down databases etc. is done this way.
– raj
Feb 17 '21 at 22:55
/etc/rc*.d
shutdown scripts.
– raj
Feb 18 '21 at 10:51
Step 1: Remind yourself who's the human here Step 2: Just go do it
/s
– mdmcnamara Feb 26 '20 at 19:16