特定服务的systemctl简短状态输出格式

18

是否有可能获取特定systemd服务的状态?

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

但没有grep,只用systemctl?像systemctl SHOW_STATUS_LIKE_A_OPTION sshd.service这样的东西

systemctl status - 太长了且有多行...

4个回答

20

你可以尝试运行 systemctl is-active sshd.servicesystemctl is-enabled sshd.servicesystemctl is-failed sshd.service 命令。


3
基于Samuel答案,我提供一个简单的shell函数用于.bashrc,包括使用grep进行颜色标记的俏皮用法:
function status () {
    for name in $@; do \
      echo ${name} $(systemctl is-active ${name}) $(systemctl is-enabled ${name}); \
      done | column -t | grep --color=always '\(disabled\|inactive\|$\)'
}

祈求:

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

请注意,对于不存在的服务,is-active 将打印 inactive,而 is-enabled 将向 stderr 输出警告信息。

1

例子:

$ systemctl list-units -q amsd.service smad.service cpqIde.service cpqFca.service cpqiScsi.service cpqScsi.service
  amsd.service     loaded active running Agentless Management Service daemon
  cpqIde.service   loaded active running cpqIde MIB handler.
  cpqiScsi.service loaded active running cpqiScsi MIB handler.
  cpqScsi.service  loaded active running cpqScsi MIB handler.
  smad.service     loaded active running System Management Assistant daemon

为了避免提供“.service”后缀,我尝试使用只有服务名称的“--type service”,但是没有任何输出。
$ systemctl list-units --type service -q amsd smad cpqIde cpqFca cpqiScsi cpqScsi
$

以下方法同样有效:
(s=(amsd smad cpqIde cpqFca cpqiScsi cpqScsi); set -x; systemctl list-units -q ${s[@]/%/.service})
+ systemctl list-units -q amsd.service smad.service cpqIde.service cpqFca.service cpqiScsi.service cpqScsi.service
  amsd.service     loaded active running Agentless Management Service daemon
  cpqIde.service   loaded active running cpqIde MIB handler.
  cpqiScsi.service loaded active running cpqiScsi MIB handler.
  cpqScsi.service  loaded active running cpqScsi MIB handler.
  smad.service     loaded active running System Management Assistant daemon


我希望有一种方法可以包含“自从”字段。

1

最接近的本地命令是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'.

来源:Red Hat系统管理员文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接