3

In order to verify in a script that a service is not running I have tried

systemctl is-active some.service

which returns 'inactive' and exit code 3 if the service is not running or it does not exist.

Sometimes however it returns 'unknown' and as well exit code 3, the explanation for that can be found here: https://bugzilla.redhat.com/show_bug.cgi?id=1073481

Status is more complex function with creates a unit in systemd manager. Than you can view its status through is-active until garbage collectors eats it.

So basically, calling something like systemctl status some.service and the garbage collector both alter the result of is-active, so it is not reliable.

The recommendation in the same discussion to use

systemctl show some.service -p ActiveState

makes a "stability promise for this command".

However on one system I am getting Unit some.service could not be found. and exit code 1 for a service that does not exist while on another system I am getting ActiveState=inactive and exit code 0.

So, how would I check reliably if a service is not running?

If the exit code is not 0 or ActiveState=inactive, is that the whole truth?

1

So first you should have the exact name of the service, some times the service name is apache and the daemon name is httpd, so to avoid this you should have the exact name of the service, you can get all services with :

systemctl list-unit-files

so to improve search you can use a grep :

systemctl list-unit-files | grep "part_of_servicename_expected"

example of ambigius state httpd with apache

systemctl list-unit-files | grep -i "apache"

after this if you have the result you should make the :

systemctl status servicename.service
3
  • I know the exact name of the service, but I don't know if it exists at all or not. And I should have mentioned (just updated my question) that I want to check the state from a script, so status isn't very convenient in this case. The manpage recommends show for scripts. – Torsten Römer Aug 11 '17 at 12:15
  • But you have Unit some.service could not be found, he cat find the service, so it's not in the knowledge base of systemd. – Hamza Jabbour Aug 11 '17 at 12:20
  • Agree, but I would like to avoid having to first find out if the service exists at all but ideally just one command that says "the service does not exist or is not running". And the confusing thing is that sometimes it says 'inactive' and sometimes 'not found' when the service does not exist. – Torsten Römer Aug 11 '17 at 12:30

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.