systemd -- Start service before shutdown, poweroff,halt

Hello.
I start a service with this unit (thanks to deano_ferrari) :

[Unit]
Description=backup at shutdown
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/bin/true
ExecStop=/path/to/backup-script.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target



I tried other kind of this :

[Unit]
Description=do task before shutdown -- service : task_before_halt.service
#Conflicts=shutdown.target reboot.target halt.target
Before=shutdown.target reboot.target halt.target
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/bin/true
ExecStart=/etc/init.d/task_before_halt
TimeoutStopSec=300

[Install]
#WantedBy=multi-user.target
WantedBy=shutdown.target reboot.target halt.target

My problem is that the service start during the shutdown but not before it.

I need to use some environment variables but it seems that nothings about the session is still alive.

For now I write the most important variables in a file that I source, but I cannot be sure that they have not be modified during the session run.

Any help is welcome.

Hi
I normally append the following to any systemd service script…


/bin/sh -c "some script"

You should not need an ExecStart if you have the RemainAfterExit.

Add your script to unit dracut-shutdown.service. Replace ‘ExecStart=/bin/true’ by ‘ExecStart=/path/to/backup-script.sh’.

What exactly “before shutdown” means? Explain at which point in time after “systemctl poweroff” should this service be started?

I need to use some environment variables but it seems that nothings about the session is still alive.

What “session” do you mean here?

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’.

I will try this and give news.

What I mean is :
1°) May day is finished
2°) I shutdown my computer
3°) I turn off the light
4°) I go out.

What I want :
The system will start my backup of the day.
The system will not start the shutdown until my backup of the day is not finished.

And that what i try to do by putting

Before=shutdown.target reboot.target halt.target

I was thinking that when shutdown is triggered, my service is started and shutdown.service is waiting that my service finished.

I think i am no fully understanding the purpose of before=

What is surprising is that I used this method some time ago, but I could not find the script I used at that time.

You mean that putting

Before=shutdown.target reboot.target halt.target

Does not prevent these targets to start before my unit finished

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…