是否有可能获取特定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
- 太长了且有多行...
是否有可能获取特定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.service
,systemctl is-enabled sshd.service
和 systemctl is-failed sshd.service
命令。
.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
输出警告信息。例子:
$ 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
$ 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
最接近的本地命令是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'.