How can I trigger a systemd unit on suspend before networking is shut down?
Inspired by https://unix.stackexchange.com/a/139664/160746 I upgraded my start/stop scripts to a full blown daemon and made it listen for PrepareToShutdown signal. This is a race against NetworkManager every start/stop, but it seems to work reliably on my system.
I have uploaded my code and systemd unit at https://github.com/davidn/av.
Acccording to the systemd-suspend documentation, as well as the systemctl man page systemctl suspend
activates the suspend.target
.
systemctl list-dependencies suspend.target --after --all
shows that suspend.target
calls systemctl-suspend.service
then sleep.target
. This means when you call systemctl suspend
the default order of operations are:
suspend.target
|-systemd-suspend.service
|-sleep.target
If you placed Before=sleep.target
, then your order of operations is likely:
suspend.target
|-systemd-suspend.service
|-[custom service]
|-sleep.target
So you're service runs after systemd-suspend.service
does its thing, which is likely your issue.
You can add to your service file to get the correct results:
Before=systemd-suspend.service
After calling systemctl daemon-reload
you should be able to use systemctl list-dependencies suspend.target --after --all
to see your service appear between suspend.target
and systemd-suspend.service
. Your final order of operations should be:
suspend.target
|-[custom service]
|-systemd-suspend.service
|-sleep.target