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

systemctl short status output format for specific service

是否可以获取特定 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 - 太长且多行...

您可以尝试 systemctl is-active sshd.servicesystemctl is-enabled sshd.servicesystemctl is-failed sshd.service

基于 ,我为 .bashrc 提供了一个简单的 shell 函数,包括厚颜无耻地使用 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 将为 non-existent 服务打印 inactive,而 is-enabled 将为 stderr 打印警告。