systemctl 列出所有可能的(包括禁用的)服务

rog*_*ack 15 systemd systemctl

我有一个源自 /etc/init.d/XX 脚本的“服务”,并为其生成了一个 systemd包装器。它不会在任何运行级别自动启动,并且当我运行时systemctl --allsystemctl list-unit-files --all不会显示在任何列表中。

我的预感是,因为它没有依赖项,尚未启动,所以它没有“加载”到 systemd 中(因此未启用,未加载),因此 systemd 不会列出它。

有没有办法获取所有可能的服务的完整列表,即使是那些尚未启动且未自动启动的服务?或者做systemctl search同等的事情?

此相关问题仅询问将在启动时尝试的服务列表。

“--all”下的man 页面显示systemctl

要列出系统上安装的所有单元,请使用 list-unit-files 命令

但这些禁用的单位不会出现在 的输出中list-unit-files

rog*_*ack 10

好吧,我问了“systemd 的人”,他们说在旧版本中并没有列出所有这些,尽管手册页是这样说的:

   -a, --all
       When listing units, show all loaded units, regardless of their
       state, including inactive units. When showing unit/job/manager
       properties, show all properties regardless whether they are set or
       not.

       To list all units installed on the system, use the list-unit-files
       command instead.
Run Code Online (Sandbox Code Playgroud)

上面列出的大多数功能似乎并没有在“我的”systemd 版本systemd 219(CentOS 6)中实际实现。但它似乎在 Debian 9 上较新版本的 systemctl 中得到了修复systemd 232,该版本具有 version ,也许这就是原因。

systemd如果 init.d 脚本确实指定了运行级别但所有目录中都不存在(la 或其等效项,或者如果最初从未在其上运行过),那么您仍然可以像旧版本/etc/rc?.d正常chkconfig --del my_service操作systemctl disable my_servicechkconfig状态、启动、停止)一样控制它们对于systemd-sysv-generator旧版本的 systemd,如果您运行等,它不会[显示在列表中][2],systemctl list-units --all即使它工作正常并且仍然​​可控。或者,如果它们是正常服务但被禁用,它们似乎也不会出现在任何列表中,尽管它们仍然有效。

参考: https://lists.freedesktop.org/archives/systemd-devel/2019-May/042555.html


Nam*_*lai 7

列出系统中正在运行、活动或失败的每个已加载服务:

# sudo systemctl list-units --type=service --all
Run Code Online (Sandbox Code Playgroud)

列出系统中所有禁用的服务:

# sudo systemctl list-unit-files --type=service --state=disabled
Run Code Online (Sandbox Code Playgroud)

列出系统中所有启用的服务:

# sudo systemctl list-unit-files --type=service --state=enabled
Run Code Online (Sandbox Code Playgroud)

列出系统中处于活动状态的每个已加载服务:

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

列出系统中正在运行状态的每个已加载服务:

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

列出系统中每个处于失败状态的已加载服务:

# sudo systemctl list-units --type=service --state=failed
Run Code Online (Sandbox Code Playgroud)

列出系统中处于退出状态的每个已加载服务:

# sudo systemctl list-units --type=service --state=exited
Run Code Online (Sandbox Code Playgroud)