17
6
Note: Briefly formulated question can be found at the bottom.
I have a script which makes a backup of my files onto an external USB drive. Executing it can take a while, depending on the amount of new data I've created. I want to run it automatically on every shutdown of the system.
I'm using fedora 23 with latest updates (systemd).
I tried to achieve this in several ways, but I couldn't get it to work.
Long running process with StopExec
autobackup.service:
[Unit]
Description=Slow backup script
Requires=local-fs.target
[Service]
ExecStart=/bin/true
ExecStop=/etc/systemd/system/do_backup.sh
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multiuser.target
I activated it with systemctl enable autobackup.service
and started it with systemctl start autobackup.service
.
Part of my boot log (journalctl -b-1
):
Dez 22 17:45:27 localhost systemd[1]: Unmounted /mnt/BACKUP.
Dez 22 17:45:27 localhost do_backup.sh[4296]: At subvol /home/BACKUP.2015_12_22-17_45_25
Dez 22 17:45:27 localhost do_backup.sh[4296]: ERROR: parent subvol is not reachable from inside the root subvol.
Dez 22 17:45:27 localhost do_backup.sh[4296]: At snapshot BACKUP.2015_12_22-17_45_25
Dez 22 17:45:27 localhost do_backup.sh[4296]: ERROR: failed to dump stream. Broken pipe
Dez 22 17:45:27 localhost systemd[1]: autobackup.service: Control process exited, code=exited status=1
Dez 22 17:45:27 localhost systemd[1]: Stopped Slow backup script.
Dez 22 17:45:27 localhost systemd[1]: autobackup.service: Unit entered failed state.
Note that I didn't shorten anything in between, it really unmounted /mnt/BACKUP just before the script started, funny coincidence..
Before shutdown.target
autobackup.service:
[Unit]
Description=Slow backup script
DefaultDependencies=no
Before=shutdown.target
[Service]
ExecStart=/etc/systemd/system/do_backup.sh
Type=oneshot
systemctl edit shutdown.target
[Unit]
Requires=autobackup.service
Output is basically the same.
The Problem
I think the problem is that systemd starts my script in parallel with all the other shutdown scripts in both cases, which unmount BACKUP and deactivate the pipe infrastructure (another error I sometimes got, when the unmount wasn't fast enough).
The Question
How can I teach systemd to start my script first on shutdown, wait until it exits and then start the rest of the shutdown scripts/targets/units/whatever?
2You shouldn't need to do the
systemctl edit
.systemctl enable autobackup
has the same effect and is more idiomatic, and should work since you already have the[Install]
section in your unit. You will have to correct that typo in the target name, though. ;) – Stefan Majewsky – 2016-04-13T10:45:06.917OMG I was searching so long for a solution to calling my backup script BEFORE any drives are unmounted. Thank you! – deanresin – 2016-05-30T21:29:51.580
2This does not work for me. I don't have any mounts, I just want to make sure my script runs before other units shut down. – 2rs2ts – 2018-09-21T01:36:23.037