What return code does systemctl status return for an error in systemctl status (not the underlying service)?

Jazzepi picture Jazzepi · Jun 23, 2019 · Viewed 7.5k times · Source

http://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic.html#INISCRPTACT

According to this systemctl status has a special list of return codes which return information about the service you're querying.

If the status action is requested, the init script will return the following exit status codes.

0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99  reserved for future LSB use
100-149   reserved for distribution use
150-199   reserved for application use
200-254   reserved

That's fine, but I'm dumbfounded on the fact that 1 does not mean that systemctl status itself failed (NOT THE UNDERLYING SERVICE).

I'm writing a script that's querying the status of a service that is in the process of starting so I expect to see status code 3 a couple of times, and then 0. But what status code(s) should I be expecting when "the systemctl status command is brazoke"?

I'm tempted to just say anything 5+ is an unrecoverable error and I should bail on waiting for the service to come up, but that's not specifically what this document says. It just gives generic reservations.

If it's any help I'm using Ubuntu 16.04 and 18.04. Maybe that vendor has specific codes? I have no idea.

Answer

Scottie H picture Scottie H · Jun 26, 2019

IMHO, the exit status is there so that the scripter (i.e. you) can get the output of the status command without having to parse the output.
Ex:
$ check [command]; echo $? [command] is running 0 $
Compare to:
$ check [command1]; echo $? [command1] is not running 0 $
The only way to know if the command is running or not would be to parse the output for not. Setting the status to 0 or 1 would allow a script to read the output much easier. Of course, if the "check" command did not work, it would have to return a higher exit code.