Search code examples
systemdlsb

Systemd unit, check status with external script


The short version is:

I have a systemd unit that I want to check the return code of a script when I call:

systemctl status service.service

Long version: I had a lsb init script that did exactly that, when status was passed as parameter it called a script that checked the state of several processes and based on the return code the init system returned the state correctly of the software.

Now when adapting the script to systemd I can't find out how to configure this behaviour.


Solution

  • Short answer

    This is impossible in systemd. The systemctl status verb always does the same thing, it cannot be overrided per-unit to a custom action.

    Long answer

    You can write a foo-status.service unit file with Type=oneshot and ExecStart= pointing to your custom status script, and then run systemctl start foo-status. However, this will only provide a zero/nonzero information (any nonzero exit code will be converted to 1).

    To get the real exit code of your status script, run systemctl show -pExecMainStatus foo-status, however, if you go this far, then it is simpler to run your script directly.