0

(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?

  • 1
    It's a design issue: Most folks want fast shutdown, so cron simply does not run. Also, there is confusion is to whether an 'shutdown' target should run at reboot? (systemd designers think not, others disagree). Finally, an @shutdown would be bypassed by a power loss, kernel panic, or holding the power button -- it's not going to reliably run under all circumstances. That's why most shutdown-cleanup jobs actually run at startup instead, so you can be sure that they actually ran. – user535733 Feb 25 '20 at 21:44
  • Biggest design failure on windows: having mandatory updates during shutdown. I need to go somewhere when I shutdown and the OS should never prevent me from getting there on time – Rinzwind Feb 26 '20 at 00:08
  • You are trying to shutdown your computer because you need to go somewhere, but it needs to install updates: what do you do?

    Step 1: Remind yourself who's the human here Step 2: Just go do it

    /s

    – mdmcnamara Feb 26 '20 at 19:16
  • @Rinzwind last time windows did that, I throw it out the window (pun intended) - five years ago, since then happy with ubuntu :) – Mischa Mar 12 '20 at 14:16

2 Answers2

0

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.

Mischa
  • 321
0

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.

Scooby-2
  • 508
  • Well... basically the same reasoning could apply for @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
  • @ raj Thanks for your comment. Regarding @reboot, I agree, “Why run it manually if it can be done automatically?” The same applies to running a script to do things before shutdown and then to call the shutdown from the script. Automation is exactly what I am suggesting! If you use a script which can perform what is required and then call the shutdown, cron "@shutdown" is not needed. Such scripts are used to safely shut down databases before the systems are powered off. To answer your question "Why do we have @reboot?" To start databases automatically on power up, for example. Less typing! – Scooby-2 Feb 17 '21 at 21:32
  • But if you shut down the system from the GUI, as it is usually done, is there a way to replace the actual shutdown operation with such a script? Probably not. Therefore you need a script that is called by the system when shutdown is invoked. Probably /etc/rc*.d scripts can be used for that. Shutting down databases etc. is done this way. – raj Feb 17 '21 at 22:55
  • Yes, on Ubuntu systems you create a .desktop file and put it in ~/.local/share/applications (for a per-user basis) or /usr/share/applications (for system-wide use). See http://askubuntu.com/questions/281293/ddg#282187 To prevent users from using the usual shutdown method use the PolicyKit's local authority system. See http://askubuntu.com/questions/130520/ddg#130536 This way the users have to use the script to power off the system, and the script will do whatever you tell it to do. – Scooby-2 Feb 18 '21 at 10:46
  • That is not an actual solution. An actual solution would be to replace the action of the GUI menu entry that calls shutdown to execute the custom script. Placing an icon on a desktop that should be double-clicked to run the "shutdown" script is just a poor workaround. Real on-shutdown actions (that shut down databases etc., which you did mention) are not done this way, but via systemd or /etc/rc*.d shutdown scripts. – raj Feb 18 '21 at 10:51
  • I don't believe this the correct platform for a discussion such as this. But sure, if you want to change the default action of pressing the "Shutdown" button in the GUI, put your script(s) in /etc/rc6.d, where they will be executed in alphabetical order. Their names must start with K99. I believe I have provided a solution and now an alternative. – Scooby-2 Feb 18 '21 at 11:08