<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Thank you so much Adam for your detailed reply. Really appreciate that. I did try systemd for the past two days based on different stuff I read on the net. But I will read carefully through your reply and try again. Will report back to the list as soon as it works. <div><br></div><div>Thanks again, </div><div><br></div><div>Vijay <br><br><div dir="ltr">Vijay Sankar<div>ForeTell Technologies Limited</div><div>vsankar@foretell.ca<br><div><div><br></div></div></div></div><div dir="ltr"><br><blockquote type="cite">On Feb 4, 2023, at 1:32 PM, Adam Thompson <athompso@athompso.net> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style>@font-face { font-family: "Cambria Math"; }
@font-face { font-family: Calibri; }
@font-face { font-family: Menlo; }
p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif; }
a:link, span.MsoHyperlink { color: rgb(5, 99, 193); text-decoration: underline; }
span.EmailStyle20 { font-family: Calibri, sans-serif; color: windowtext; }
.MsoChpDefault { font-size: 10pt; }
@page WordSection1 { size: 8.5in 11in; margin: 1in; }
div.WordSection1 { page: WordSection1; }</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->


<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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…<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Repeating stuff you probably already know, but saying it out loud for everyone’s benefit.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">You need two pieces – a script, and a systemd unit file.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
<tbody>
<tr>
<td valign="top" style="border:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">/etc/systemd/system/last-gasp.service<o:p></o:p></span></p>
</td>
</tr>
<tr>
<td valign="top" style="border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">[Unit]<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Description=script that does stuff<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">DefaultDependencies=no<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Before=shutdown.target<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">[Service]<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Type=oneshot<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">ExecStart=/some/where/lastgasp.sh<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">TimeoutStartSec=0<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">[Install]<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">WantedBy=shutdown.target<o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">(Unit file syntax reference docs:
</span><a href="https://www.freedesktop.org/software/systemd/man/systemd.service.html">systemd.service (www.freedesktop.org)</a> and
<a href="https://www.freedesktop.org/software/systemd/man/systemd.unit.html">systemd.unit (www.freedesktop.org)</a>)<o:p></o:p></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Make sure the script is executable or this will fail (more or less) silently.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Run “systemctl daemon-reload” to make it notice your new unit file.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Run “systemctl enable last-gasp” to ask systemd to, y’know, actually *do*<b>
</b>something with it at shutdown.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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
</span><a href="https://www.freedesktop.org/software/systemd/man/systemd-halt.service.html">systemd-poweroff.service (www.freedesktop.org)</a>.)  This approach likely happens too late in the process to be useful, but read the docs and assess for yourself.<span style="mso-fareast-language:EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">-Adam<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Roundtable <roundtable-bounces@muug.ca>
<b>On Behalf Of </b>vsankar@foretell.ca<br>
<b>Sent:</b> Saturday, February 4, 2023 12:40 PM<br>
<b>To:</b> roundtable@muug.ca<br>
<b>Subject:</b> [RndTbl] How can one execute a script just before shutdown on Ubuntu<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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 (<span style="font-size:8.5pt;font-family:"Menlo",serif">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)</span>?<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I created a simple script <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p style="margin:0in;font-stretch: normal"><span style="font-size:8.5pt;font-family:"Menlo",serif">-rwxr-xr-x 1 root root 51 Jan 30 11:46 <b><span style="color:#2FB41D">/etc/init.d/before-shutdown</span></b><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Then did a link so that I had<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p style="margin:0in;font-stretch: normal"><span style="font-size:8.5pt;font-family:"Menlo",serif">lrwxrwxrwx 1 root root 25 Jan 30 11:47
<b><span style="color:#2EAEBB">K04before-shutdown</span></b> -> <b><span style="color:#2FB41D">../init.d/before-shutdown</span></b><o:p></o:p></span></p>
<p style="margin:0in;font-stretch: normal"><span style="font-size:8.5pt;font-family:"Menlo",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal">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 <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p style="margin:0in;font-stretch: normal"><span style="font-size:8.5pt;font-family:"Menlo",serif">lrwxrwxrwx 1 root root 25 Feb  4 11:58
<b><span style="color:#2EAEBB">/etc/rc6.d/K99before-shutdown</span></b> -> <b><span style="color:#2FB41D">../init.d/before-shutdown</span></b><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This was a complete fail as well. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Would really appreciate any suggestions on how to make this work. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks very much,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Vijay<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Vijay Sankar<o:p></o:p></p>
</div>
<p class="MsoNormal">ForeTell Technologies Limited<br>
<a href="mailto:vsankar@foretell.ca">vsankar@foretell.ca</a> <o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>


<span>_______________________________________________</span><br><span>Roundtable mailing list</span><br><span>Roundtable@muug.ca</span><br><span>https://muug.ca/mailman/listinfo/roundtable</span><br></div></blockquote></div></body></html>