18

is it possible to get status for specific systemd service

$ systemctl -a | grep sshd.service
  sshd.service              loaded    active   running   OpenSSH server daemon
$

but without grep, only with systemctl ? Something like systemctl SHOW_STATUS_LIKE_A_OPTION sshd.service

systemctl status - too long and multiline...

Dmitry Perfilyev
  • 536
  • 1
  • 4
  • 14

3 Answers3

19

You can try systemctl is-active sshd.service, systemctl is-enabled sshd.service and systemctl is-failed sshd.service.

2

Based on Samuel's answer, I offer a simple shell function for .bashrc, including cheeky use of grep for colorization:

function status () {
    for name in $@; do \
      echo ${name} $(systemctl is-active ${name}) $(systemctl is-enabled ${name}); \
      done | column -t | grep --color=always '\(disabled\|inactive\|$\)'
}

Invocation:

> status ssh ntp snapd
ssh    active    enabled   
ntp    active    enabled   
snapd  inactive  disabled  

Note that is-active will print inactive for non-existent services, while is-enabled will print a warning to stderr.

Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
0

The closest is native command is systemctl list-units -t service:

$ systemctl --user list-units --type service
  UNIT                    LOAD   ACTIVE SUB     DESCRIPTION
  dbus.service            loaded active running D-Bus User Message Bus
  podman-promtail.service loaded active running rootless pod promtail

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
2 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Source: Red Hat sys admin docs

Jarppiko
  • 11
  • 1