Linux – Systemd: stop service before suspend, restart after resume

arch linuxsystemd

I have a systemd service that runs a background daemon (xflux) from the user session manager.

When I suspend and resume my computer, it messes up xflux's time reckoning; I have to manually issue systemctl --user restart xflux to fix it.

How can I tell the service to automatically stop before suspend, and start after resume? (I found an answer that automatically runs a given service on resume, but this isn't quite what I want.)

Best Answer

You can put an executable file in /lib/systemd/system-sleep/ and it will be run on suspend with the 2 args pre and suspend, and again after resume with post and suspend. The systemd man page says this is a hack.


To use a systemd Unit, see my later answer to a similar question.

Related Question