5

I have a systemd service controlling some hardware, let's take lamps as a good example. It is OK to restart the service, or reboot the PC, because it is safe that the lamps are left switched on for some while.

However if (and only if) the system is going to halt (power off), I want to do not only the regular cleanup, but also to switch the lights off, i.e. to stop the service in a sligthly different way.

In all mentioned cases (service restart, reboot, halt) the ExecStop command is executed the same way. How can I distinguish between these cases or otherwise know if systemd is doing poweroff or not?

0

This answer explain how to use the power target

You can add a service that will trigger a command on power-off event, something like

[Unit]
Description=My command on power event
Before=shutdown.target
DefaultDependencies=no

[Service]
ExecStart=/some/thing/to/execute
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
  • Thank you. The link to this was already posted in the comments. While this is helpful, the best solution for me would allow the ExecStop command of the running service to check in which situation it was called. – VPfB Jun 2 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.