Systemctl 命令显示正在运行的服务摘要

Systemctl 命令显示正在运行的服务摘要

我应该使用哪个systemctl选项或命令来显示当前正在运行的所有服务的摘要?

答案1

您可以使用的一些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

答案2

是的(见man 1 systemctl):

systemctl list-units | grep -E 'service.*running'

或(另请参阅man 8 service

service --status-all

其中[+]表示实际正在运行的服务。

答案3

经过长时间的寻找,我想出了这种略有不同的方法来确定正在运行的服务。它还展示了如何计算正在运行的服务数量。这种方法确保它不会意外地捕获服务名称本身带有单词 running 或 service 的内容。虽然这种方法可能有点老套,但它迫使你学习正则表达式和 GNU/BSD 提供的最好的 awk/sed。

# 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}'

相关内容