Score:0

Trying to get timing of systemd script right

se flag

I have this script that I'm trying to get the system to execute on shutdown and only on shutdown (or restart for that matter). This script doesn't start or stop a server; it merely unmounts some file systems. Unfortunately a lot of the documentation I find seems to think I'm going to write a script to manage a server, but that's not what I'm doing at all.

Reading http://tuxgraphics.org/npa/systemd-scripts/ and man systemd.service and man systemd.unit (I'm not even sure what I'm supposed to be following) I have:

[Unit]
Description=My umount script
After=network.target syslog.target

[Service]
Type=oneshot
ExecStop=/home/PL/bin/umountb2.sh
StandardOutput=journal

But I have no idea if that's right and I'm even less confident that it will be executed at the right time. I need it to execute sometime after all the regular users are logged out but not before any file systems are unmounted.

I'm running Ubuntu 22.04.2

Edit: now the config file looks like this:

 Description=My special shutdown script

 [Service]
 Type=oneshot
 RemainAfterExit=true
 ExecStop=/home/PL/bin/umountb2.sh

 [Install]
 WantedBy=multi-user.target

And things have improved, but there's a problem, because my script calls cryptsetup which returns an error. I think it's being executed too late, because when I restart, I run journalctl -b -1 --dmesg there are only 3 messages that follow my script, the first being:

kernel: nfsd: last server has exited, flushing export cache

pl flag
Why do you need to even do this unmounting? The filesystem will get unmounted during the shutdown process anyway.
Opux avatar
se flag
@popey Not if it mounts a file-based filesystem. It needs to first unmount that file system, then close the device in /dev, then it can proceed to unmount everything else
Doug Smythies avatar
gn flag
I think [this](https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown) might help.
Opux avatar
se flag
@DougSmythies maybe it did a little. How can I know my script is being executed? I had some log messages expressed w/`echo` piped to `>&2` and some made w/`logger` but I don't see any of these messages in the logs
Doug Smythies avatar
gn flag
While I haven't tried it for awhile, I use `echo "doug_script $FWVER done..." >> /dev/kmsg` and then see it in `sudo dmesg`, but that gets scrubbed duing reboot (I think).
Doug Smythies avatar
gn flag
after you boot again, and doing the above type log entry, try `sudo journalctl -b -1 --dmesg | grep doug_script`, changing the search parameter to whatever you used. It worked for me.
Opux avatar
se flag
@DougSmythies and that's after redirecting it to /dev/kmsg?
Doug Smythies avatar
gn flag
Yes. your redirect to /dev/kmsg is done during the script execution during shutdown, hopefully before kernel logging has been terminated. Then you should be able to see the message the next time you start the computer using the journalctl command.
Opux avatar
se flag
@DougSmythies Yep. That did it. Thx
Opux avatar
se flag
@DougSmythies Actually, there's still a problem and I think it's down to timing. I've edited my question which proposes the script be called earlier.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.