我知道运行systemctl命令本身会列出所有服务,但我只想获取已启用的服务。
systemctl list-unit-files | grep enabled
将列出所有已启用的单位文件。
如果您想知道当前正在运行的单位文件,您需要使用 systemctl | grep running
。
使用您要查找的那个。已启用并不意味着它正在运行。运行并不意味着它已启用。它们是两个不同的概念。
已启用意味着系统将在下次启动时运行该服务。因此,如果您启用了一个服务,仍然需要手动启动它,或者重新启动系统以启动它。
正在运行意味着它实际上正在运行,但如果它未启用,则在重新启动时将不会重新启动。
systemctl | grep running
对我来说没有列出任何内容!即使有东西在运行,也只会以其状态如“已启用、已禁用、已屏蔽、静态”进行列出。 - Cirelli94systemctl | grep -E 'running.*active|active.*running' | grep mys
命令来查看 mysql 服务是否正在运行。 - Eric Hepperle - CodeSlayer2010systemctl
没有其他选项时,它会显示一个输出,其中第四列说明某个进程是否正在运行/失败/退出等等。你确定你是在运行Ubuntu 16.04吗? - Deloreansystemctl | grep mysql
就可以查看它是否正在运行。 - Deloreansystemctl list-unit-files --state=running
- Willsystemctl list-unit-files
并没有列出所有的服务。例如,它没有列出 tinc
,尽管 tinc 是已启用的。也许 systemctl list-unit-files
只列出了有单元文件的服务?但是它没有列出任何可能通过传统 sysv 风格的 init 脚本启用的服务。 - mpbsshd.service
的单元文件,但该单元实际上被称为ssh.service
。服务模板也是如此。 - OrangeDogmy@x.service
并禁用了my@y.service
。从systemctl list-unit-files
中你只会看到my@.service indirect
,这是无用的。 - Walf--all
参数。 - digital_infinity--all
选项来列出所有实例的状态或启动它们。是否使用了通配符?(systemctl list-unit-files 'my@*' --all
) - digital_infinity--all
在 status
上正常工作了,但在 list-unit-files
上却不起作用。 - digital_infinitylong_running_backup.service
或gpu_enabled_calculation.service
将导致未来出现错误。这是Unix哲学的最佳体现。 - ominug--no-legend
,并给 grep 添加 -w
参数。 - ominugman systemctl
的说明如下:
--state=
参数应为逗号分隔的单元
LOAD
、SUB
或ACTIVE
状态的列表。在列出单元时,仅显示指定状态的单元。使用--state=failed
仅显示失败的单元。
解释:
LOAD
:反映单元定义是否已正确加载。
ACTIVE
:高级单元激活状态,即 SUB
的概括。
SUB
:低级单元激活状态,值取决于单元类型。
您也可以使用此选项仅显示 enabled
单元:
systemctl list-unit-files --state=enabled
systemctl enable --now ...
- Mmmh mmh--state=enabled
在 systemd 版本 215(在 Raspbian 8 Jessie 上)上没有效果,但在 systemd 版本 229(在 Ubuntu 16.04.03 Xenial 上)上有效。 - mpb--state=generated
以查看可能正在运行的生成的单元文件(例如,pihole-FTL服务就是这样工作的)。 - mgalgs--type service
的服务类型过滤器,只能筛选出该类型的单元。list-unit-files
现在包括生成的单元。链接servicename@.service
的,而--list-units
命令则不会显示该服务。 - Mat Msystemctl list-units --type=service --state=running
列出所有 systemd
服务,其状态为 active
,并且子状态为运行中或已退出
systemctl list-units --type=service --state=active
servicename@.service
,则显示该服务(们) +1 - Mat Msystemctl list-unit-files --type service --state enabled,generated
systemctl list-units --type service --state running
sshd
与ssh
,以及syslog
与rsyslog
。 - OrangeDoglist-unit-files
现在包括生成的单元,详情请参见:https://lists.freedesktop.org/archives/systemd-devel/2019-May/042555.html - Pablo Bianchisystemctl list-units --type service --state running,failed
systemctl list-units --type=service --state=active,running | awk '/.*\.service/ {print $1}'
要列出模板化的单元,您可以使用以下命令:
systemctl list-units --all|grep yourservice
--all
开关还会显示所有已实例化的单元,这些单元是通过以下方式实例化的:
systemctl <service_name>@<argument>.service
来自man手册的说明:
当使用list-units列出单元时,还会显示非活动单元和跟随其他单元的单元。在显示单元/作业/管理器属性时,无论是否设置,都会显示所有属性。
示例:
systemctl openvpn@my_office_endopoint.service
进一步阅读: https://fedoramagazine.org/systemd-template-unit-files/
列出 用户 otheruser
的 活动用户 服务:
sudo runuser -l otheruser-c "systemctl --user list-units --type=service --state=active"
sudo shutdown -r now
systemctl list-units --type=service --state=running,enabled,failed,generated
man systemctl
。 - Jos