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


12

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


您应该接受@Zanna的回答。即使它也是一种有效的方法,也比我的方法更能解决您的问题。
Videonauth

Answers:


20

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

3
systemctl没有任何子命令假定list-units,所以...... systemctl --type-service --state=running,或只是一个普通的systemctl快速使用。
muru


4

在查找了超出必要的时间之后,我想到了确定运行服务的这种略有不同的方法。它还显示了如何计算正在运行的服务数量。这样可以确保它不会在服务名称本身中意外地捕获“运行”或“服务”一词。

# 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}'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.