0

Is there a way to start another service (or run a script) when my service enters inactive (dead) state ?

For e.g., in upstart, I had

 start on stopped <otherjob>

In systemd, I see on OnFailure= - which is for cases when the service fails to start. In my case, the daemon in running, but then gets killed (due to a signal, say), how can I invoke another service (trigger start of another systemd service unit or a script)? .

Note: my service has Restart=no, if I check in a script invoked from ExecStopPost= then I cannot distinguish between SIGTERM sent by user or other process vs SIGTERM sent by systemd. I would rather want systemd to do the script/service invocation when my service dies (goes inactive/dead)

Ani
  • 32
  • 2
  • 13
  • It's not the cleanest solution, but you could create a separate timer unit and a script that checks your service periodically. If it detects it being down, the script should execute the other service. – aardbol Aug 01 '20 at 12:43
  • Do you actually need to start some other service? Maybe you actually just need to do some one-off stuff before restarting the original service. Can you give some more context on how these services are meant to interact? – Michael Hampton Aug 01 '20 at 14:45
  • This is confusing. If you _always_ want another service when your first service finishes (inactive/dead is finished) add `Wants=` and `Before=` to your first service and refer your second service. Otherwhise use `OnFailure=`. Note that there is `$EXIT_STATUS` and `$EXIT_CODE` on `ExecStopPost=` with systemd version >= v232 (you can hack a handler for this) – Beko Pharm Aug 01 '20 at 20:15
  • @EarthMind I didn't think of that, maybe that's an option too. But how would I stop this timer unit? (Can it be part of my main unit so that when that stops, the timer will stop too?) – Ani Aug 02 '20 at 13:13
  • @Michael, you're correct - I have some actions to perform before restarting the unit under question and the suite of services it belongs to. For e.g I have my set of services, say A, of which this unit B is critical, when it's stopped/killed/crashes I need to take some actions, cleanups, checks etc. And then restart all the services of the suite A. – Ani Aug 02 '20 at 13:16
  • @Beko, tried that, doesn't help! Can't distinguish between `SIGTERM` sent by `systemd` vs some other rogue daemon/user sending `SIGTERM` to my daemon (in `ExecStopPost` handler). – Ani Aug 02 '20 at 13:18
  • The timer is not the issue on that case. Your timer should just keep on running to check if your service is up or down. Your script should be the one that only starts the other service if the first one is down. If it's up it should do nothing – aardbol Aug 02 '20 at 13:30

0 Answers0