Answers:
您可以使用的一些systemctl
选项:
-t, --type=
The argument should be a comma-separated list of unit types such as
service and socket.
If one of the arguments is a unit type, when listing units, limit
display to certain unit types. Otherwise, units of all types will
be shown.
As a special case, if one of the arguments is help, a list of
allowed values will be printed and the program will exit.
--state=
The argument should be a comma-separated list of unit LOAD, SUB, or
ACTIVE states. When listing units, show only those in the specified
states. Use --state=failed to show only failed units.
As a special case, if one of the arguments is help, a list of
allowed values will be printed and the program will exit.
所以可能您想要:
systemctl --type=service --state=active list-units
其中列出了所有活动的服务,包括已退出的服务。如果您仅在此时运行这些程序,则可以使用:
systemctl --type=service --state=running list-units
systemctl
没有任何子命令假定list-units
,所以...... systemctl --type-service --state=running
,或只是一个普通的systemctl
快速使用。
它是(请参阅man 1 systemctl
):
systemctl list-units | grep -E 'service.*running'
或(另请参见man 8 service
)
service --status-all
凡[+]
表示其实际运行的服务。
在查找了超出必要的时间之后,我想到了确定运行服务的这种略有不同的方法。它还显示了如何计算正在运行的服务数量。这样可以确保它不会在服务名称本身中意外地捕获“运行”或“服务”一词。
# Output all active services:
systemctl -t service --state=active --no-pager --no-legend
# Count of all active services:
systemctl -t service --state=active --no-pager --no-legend | grep -c -
# Output all running services:
systemctl -t service --state=active --no-pager --no-legend | egrep '^*\.service.*running'
# Count of all running services:
systemctl -t service --state=active --no-pager --no-legend | egrep '^*\.service.*running' -c -
# Output only the service and its description:
systemctl -t service --state=active --no-pager --no-legend | egrep '^*\.service.*running' | awk 'BEGIN { FS = " ";} {for (i = 2; i <= 4; i++) { $i = "" }; print}'