[RndTbl] How can one execute a script just before shutdown on Ubuntu

Adam Thompson athompso at athompso.net
Sat Feb 4 13:31:54 CST 2023


You pretty much have to do it through a SystemD unit nowadays.  I’ve found the backwards-compatibility bits to be mildly unreliable, to put it, er, mildly…
Repeating stuff you probably already know, but saying it out loud for everyone’s benefit.

You need two pieces – a script, and a systemd unit file.
The unit file goes in /etc/systemd/system/ and looks like any other unit, pointing to a script that from systemd’s POV is just another executable.  For example:

/etc/systemd/system/last-gasp.service
[Unit]
Description=script that does stuff
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/some/where/lastgasp.sh
TimeoutStartSec=0

[Install]
WantedBy=shutdown.target
(Unit file syntax reference docs: systemd.service (www.freedesktop.org)<https://www.freedesktop.org/software/systemd/man/systemd.service.html> and systemd.unit (www.freedesktop.org)<https://www.freedesktop.org/software/systemd/man/systemd.unit.html>)

Make sure the script is executable or this will fail (more or less) silently.

Run “systemctl daemon-reload” to make it notice your new unit file.
Run “systemctl enable last-gasp” to ask systemd to, y’know, actually *do* something with it at shutdown.

Test with “reboot”.  If the script takes long enough to run (hint: add “sleep 30” to make it run long enough!) you’ll see systemd print something on screen about waiting for the task to complete.
I think the /etc/systemd/system/ path is correct on pretty much any Linux, but the proof is in the pudding – if the “enable” step works, then the unit file is in the correct location, or at least a correct-enough location.

Note that systemd may have unmounted the filesystem where your script is located before it tries to run the script; put the script somewhere on the root filesystem to work around this, if needed.
In theory you can add “RequiresMountsFor=/some/where” to the [Unit] section of the unit to ensure the filesystem doesn’t get unmounted until the script exits – never tried it.
Also, even if the filesystem is still mounted, it may be mounted read-only at this point, so you may not be able to write anything to disk.  This appears to be undefined, I can’t find anything that documents whether RequiresMountsFor leaves you with read-write or read-only mounts.

IN THEORY you can also just drop your script into /usr/lib/systemd/system-shutdown/ and magic will happen, but… dunno, I’ve never tried that.  Let us know if it actually works?  (See systemd-poweroff.service (www.freedesktop.org)<https://www.freedesktop.org/software/systemd/man/systemd-halt.service.html>.)  This approach likely happens too late in the process to be useful, but read the docs and assess for yourself.
-Adam

From: Roundtable <roundtable-bounces at muug.ca> On Behalf Of vsankar at foretell.ca
Sent: Saturday, February 4, 2023 12:40 PM
To: roundtable at muug.ca
Subject: [RndTbl] How can one execute a script just before shutdown on Ubuntu

Sorry to bother you all with this question — can you please point me to a helpful document or give me some instructions on how to execute a script just before shutting down an Ubuntu system (Linux vijay-iMac 5.15.0-58-generic #64-Ubuntu SMP Thu Jan 5 11:43:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux)?

I created a simple script


-rwxr-xr-x 1 root root 51 Jan 30 11:46 /etc/init.d/before-shutdown

Then did a link so that I had


lrwxrwxrwx 1 root root 25 Jan 30 11:47 K04before-shutdown -> ../init.d/before-shutdown


Nothing happened!! So I wasted a lot of time reading up on systemd etc., and set up unit files to execute before shutdown.target, network.target, power off.target, as well as halt.target. Since these did not work either and life is too short, I went back to what I thought was the old way of doing things and did a


lrwxrwxrwx 1 root root 25 Feb  4 11:58 /etc/rc6.d/K99before-shutdown -> ../init.d/before-shutdown

This was a complete fail as well.

Would really appreciate any suggestions on how to make this work.

Thanks very much,

Vijay


Vijay Sankar
ForeTell Technologies Limited
vsankar at foretell.ca<mailto:vsankar at foretell.ca>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://muug.ca/pipermail/roundtable/attachments/20230204/7b342807/attachment-0001.htm>


More information about the Roundtable mailing list