I have a simple backup application, in the form of a bash shell script, which has to run immediately the user shuts down. I have a systemd service “run_at_shutdown.service” which calls the script via the “ExecStop” directive. The bash script then uses tar, rsync, etc to copy files from the local machine to a couple of local (LVM) drives, mounted via fstab: the drives are /dd and /e
The problem is that the tar-ing and rsync-ing takes a couple of minutes, during which time systemd has blithely thundered on with the shutdown, so that either or both of the required network drives have already been unmounted when they are needed.
I have implemented various bits of advice found on the web, including: an “After” dependency on network.target (which during shutdown should act as a “before” requirement); listing the required mounts in the “RequiresMountsFor” directive; and “RemainAfterExit”, but still the problem persists. How do I debug this? Is there some way I can make systemd list out exactly what it’s doing and why?
Or can anyone suggest something else to try?
Here is my service definition:
$ systemctl cat run_at_shutdown.service
# /etc/systemd/system/run_at_shutdown.service
[Unit]
Description=Run programs before shutdown
RequiresMountsFor=/home /e /dd /shared /mnt/WinW
Wants=system_backups.service
After=network.target
[Service]
Type=oneshot
User=myuser
Group=users
ExecStop=/usr/local/sbin/run_at_shutdown.sh
# This calls backup_and_replicate.sh, referred to in the log
RemainAfterExit=yes
TimeoutSec=480
[Install]
WantedBy=multi-user.target
And here’s an extract from the system log, showing an example of the failure
Dec 05 21:10:20 ceres myuser[2787]: /usr/local/sbin/backup_and_replicate.sh (2780) 2.001 running
...
Dec 05 21:10:21 ceres systemd[1933]: Stopped target Basic System.
...
Dec 05 21:10:21 ceres blkdeactivate[2655]: [UMOUNT]: unmounting vg02-e (dm-4) mounted on /e... done
Dec 05 21:10:21 ceres blkdeactivate[2655]: [UMOUNT]: unmounting vg01-dd (dm-2) mounted on /dd... done
...
Dec 05 21:10:21 ceres myuser[2880]: backup_and_replicate.sh (2780) tar-ing /dd/mozilla/Thunderbird/W
Dec 05 21:10:21 ceres run_at_shutdown.sh[2881]: tar: /e/W/TBird.tar: Cannot open: No such file or directory
Dec 05 21:10:21 ceres run_at_shutdown.sh[2881]: tar: Error is not recoverable: exiting now
...
Dec 05 21:10:21 ceres run_at_shutdown.sh[2888]: rsync: [sender] change_dir "/dd/mozilla/Thunderbird/W" failed: No such file or directory (2)
... etc
This is Mint 21.3