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

use*_*368 16 services systemd

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

Zan*_*nna 25

您可以使用systemctl's 的一些选项:

-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.
Run Code Online (Sandbox Code Playgroud)

所以可能你想要:

systemctl --type=service --state=active list-units
Run Code Online (Sandbox Code Playgroud)

其中列出了所有活动的服务,包括那些已经退出的服务。如果您只关注此时正在运行的那些,您可以使用:

systemctl --type=service --state=running list-units
Run Code Online (Sandbox Code Playgroud)

  • 没有任何子命令的 `systemctl` 命令假定为 `list-units`,所以... `systemctl --type-service --state=running`,或者只是一个简单的 `systemctl` 以便快速使用。 (4认同)

Vid*_*uth 12

它是(见man 1 systemctl):

systemctl list-units | grep -E 'service.*running'
Run Code Online (Sandbox Code Playgroud)

或(另见man 8 service

service --status-all
Run Code Online (Sandbox Code Playgroud)

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


Tra*_*ard 5

在环顾四周超过必要的时间后,我想出了这种确定正在运行的服务的略有不同的方法。它还展示了如何计算正在运行的服务的数量。这种方式确保它不会意外地捕获服务名称本身中带有 running 或 service 一词的内容。

# 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}'
Run Code Online (Sandbox Code Playgroud)