62

My problem:

I'm writing a bash script and in it I'd like to check if a given service is running.

I know how to do this manually, with $ service [service_name] status.

But (especially since the move to systemd) that prints a whole bunch of text that's a little messy to parse. I assumed there's a command made for scripts with simple output or a return value I can check.

But Googling around only yields a ton of "Oh, just ps aux | grep -v grep | grep [service_name]" results. That can't be the best practice, is it? What if another instance of that command is running, but not one started by the SysV init script?

Or should I just shut up and get my hands dirty with a little pgrep?

96

systemctl has an is-active subcommand for this:

systemctl is-active --quiet service

will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts:

systemctl is-active --quiet service && echo Service is running

If you omit --quiet it will also output the current status to its standard output.

As pointed out by don_crissti, some units can be active even though nothing is running to provide the service: units marked as “RemainAfterExit” are considered active if they exit successfully, the idea being that they provide a service which doesn’t need a daemon (e.g. they configure some aspect of the system). Units involving daemons will however only be active if the daemon is still running.

22

systemctl does have a mode suitable for scripting; use show rather than status, and add the -p / --properties and --value options to get only the output you want.

Here's an example (from an Ubuntu 17.04 system):

$ systemctl show -p SubState --value NetworkManager
running

Running (or otherwise) is a SubState. If you want to know whether a service is active, use the property ActiveState

$ systemctl show -p ActiveState --value x11-common
inactive
$ systemctl show -p SubState --value x11-common
dead

Notes from the man:

show [PATTERN...|JOB...]
           Show properties of one or more units, jobs, or the manager
           itself. If no argument is specified, properties of the
           manager will be shown. If a unit name is specified, properties
           of the unit are shown, and if a job ID is specified,
           properties of the job are shown. By default, empty properties
           are suppressed. Use --all to show those too. To select specific
           properties to show, use --property=. This command is intended
           to be used whenever computer-parsable output is required. Use
           status if you are looking for formatted human-readable output.

-p, --property=
           When showing unit/job/manager properties with the show command,
           limit display to properties specified in the argument. The
           argument should be a comma-separated list of property names,
           such as "MainPID". Unless specified, all known properties are
           shown. If specified more than once, all properties with the
           specified names are shown. Shell completion is implemented for
           property names.

--value
           When printing properties with show, only print the value, and
           skip the property name and "=".
  • 2
    +1 for sophisticated answer . kindly specify the distros which will accept --version option with systemctl. – S.K. Venkat Apr 26 '18 at 11:31
9

As a complement to Zanna's answer, the --value option for systemctl show has been introduced with version 230 of systemd. So it may not be available on certain distros like debian jessie.

In this case, one can emulate the option by using sed:

$ systemctl show -p ActiveState sshd | sed 's/ActiveState=//g'
active
$ systemctl show -p SubState sshd | sed 's/SubState=//g'  
running
  • 1
    +1 for pointed out the --value intro version & distro which will not work. – S.K. Venkat Apr 26 '18 at 11:29
2

Instead of using the sed command like in the answer of Oxmel, it is enough to use cut -d'=' -f 2 for all kind of properties queried:

for example:

$ systemctl show -p ActiveState sshd | cut -d'=' -f2
active
$ systemctl show -p SubState sshd | cut -d'=' -f2
running
1

i find this useful for command line execution or if you are making scripts.

Copied from @StephenKitt

This will check if the service is down and perform service restart

systemctl is-active --quiet <service name> || <service name> restart

the || there checks if the return value from systemctl is non-zero meaning if it's not active as explained by the author.

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.