A systemd Unit has two parameters for starting the Unit – “Before=” and “After=” …
After= is the inverse of Before=, i.e. while After= ensures that the configured unit is started after the listed unit finished starting up, Before= ensures the opposite, that the configured unit is fully started up before the listed unit is started.
Note that when two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied. i.e. if a unit is configured with After= on another unit, the former is stopped before the latter if both are shut down. Given two units with any ordering dependency between them, if one unit is shut down and the other is started up, the shutdown is ordered before the start-up. It doesn’t matter if the ordering dependency is After= or Before=, in this case. It also doesn’t matter which of the two is shut down, as long as one is shut down and the other is started up. The shutdown is ordered before the start-up in all cases.
You could define a Unit which is started at some point in time before shutdown but, after the time when the Multi-User state was exited …
Looking through the systemd “Special units” – man 7 systemd.special – I can’t see anything which would be suitable for the required point in time – they all seem to be defined for booting and shutting down …
Ordering won’t help here, as shutdown will proceed concurrently. You actually can start the unit unconditionally, perform the backup e.g. ‘ExecStop=/usr/bin/rsync -a --exclude=.cache /home/ /HDD/’ and have something like ‘Conflicts=shutdown.target reboot.target halt.target umount.target’.
Hi
Not sure why you don’t just use a ‘user’ systemd service to run you backup script and when it’s finished shut the system down… Have a desktop file to run it when you want to shutdown with a backup, else use the normal shutdown…