列出在启动时启动的服务的命令是什么?

有没有一个命令可以列出在启动时运行的服务?我想这可能涉及解析"/etc/init.d/"和各个"/etc/rc.*"目录。
9个回答

快速答案是:这取决于您的init系统。
详细回答是:对于当前版本的Ubuntu,您可能有一个UpstartSystemV混合体。在15.04“活力狮”之后的新版Ubuntu(以及其他Linux发行版,如RHEL / CentOS 7)正在使用SystemD

Upstart

要列出所有服务:

sudo initctl list

要列出所有的Upstart服务并在它们上运行initctl show-config,这个一行命令可能会有帮助:
sudo initctl list | awk '{ print $1 }' | xargs -n1 initctl show-config

System V

列出所有服务:

sudo service --status-all

或:

# for init scripts:
ls /etc/init.d/

# for runlevel symlinks:
ls /etc/rc*.d/

SystemD

列出所有服务:

sudo systemctl --all list-unit-files --type=service

或者:

ls /lib/systemd/system/*.service /etc/systemd/system/*.service

19这应该是被接受的答案。 - sjas
4service --status-all 不会显示服务是否已启用以在启动时运行,至少在Ubuntu 16上不会。它显示服务当前是否正在运行。 - Wildcard
1我必须使用sudo service --status-all命令才能显示出所有的服务。当我在非root账户上运行service --status-all命令时,有几个服务是隐藏的。 - Phlucious
@Phlucious:谢谢你提到这一点。我以为这些命令通常是以root身份运行的(systemctlserviceinitctl...),因为它们通常被视为系统管理命令。 - TrinitronX
service --status-all 这个命令在我的 debian 系统上也起作用。 - Arun
OP要求在启动时提供服务,因此您需要使用--state enabled参数来执行systemctl命令。而且根据man页面,service --status-all命令返回服务是否正在运行,而不是它们是否设置为在启动时运行。 - Pablo Bianchi

你可以直接使用initctl list命令来列出/etc/init的内容,而不是建议的dbus-send命令。

12这在Ubuntu 18.04上能用吗?我在bash中收到了"initctl: command not found"的错误提示。 - Sanjay Manohar
8在Ubuntu 19.10上找不到initctl list命令。 - Rémy Hosseinkhan Boucher
4@RémyHosseinkhanBoucher 有关Ubuntu的最新版本,请访问https://askubuntu.com/a/1167921/988056。 - AppyGG

对于Ubuntu 18.04,请使用以下命令:
systemctl list-units --type=service

而不是使用initctl

自Ubuntu 16.04起,initctl已被systemd取代(来源,法文)。

如果可以的话帮助@sanjay-manohar


“/etc/init.d”和“/etc/rc.*”目录已被新的“upstart”初始化工具取代。尽管这些目录中的脚本将按预期执行,但现在运行init的新方法是由“/etc/init/”中的文件定义的。
您可以通过查询dbus上的upstart来列出所有的upstart作业:
dbus-send --print-reply --system --dest=com.ubuntu.Upstart \
        /com/ubuntu/Upstart com.ubuntu.Upstart0_6.GetAllJobs

你可能需要将0_6更改为您拥有的upstart版本。这个命令适用于我安装的lucid版本。

3@Eric H:你能把下面的答案设为正确吗?initctl list比这个dbus命令要好得多。虽然我想保留这个答案供参考(而不是完全删除它)。 - Jeremy Kerr

如果你想要一个漂亮的图形化展示服务和启动时间,可以尝试:
sudo apt install bootchart

对于systemd(自16.04开始),请尝试使用 systemd-bootchart

sudo apt install systemd-bootchart

我会使用initctl show-config <servicename>来真正了解您的服务在启动过程中何时/是否启动。
就像这样:
$ initctl show-config myservice
myservice
  start on runlevel [2345]
  stop on runlevel [!2345]

或者对于NFS4的idmap-daemon:
$ initctl show-config idmapd
idmapd
  start on (local-filesystems or mounting TYPE=nfs4)
  stop on runlevel [06]

chkconfig在我看来,只在基于RedHat的系统上更可取。

1这是正确的答案。我不知道为什么所有错误和不完整的答案都获得了那么高的赞同票。 - Cerin
1这对使用SysV的人来说不起作用,我同意这是一个很好的答案,但它还不完整。 - Gabriel Netto

12.04那天我们可以使用:
sudo apt-get install chkconfig
chkconfig --list

但是在12.10版本中,它已经被移除了。示例输出:
acpi-support              0:off  1:off  2:on   3:on   4:on   5:on   6:off
acpid                     0:off  1:off  2:off  3:off  4:off  5:off  6:off
apparmor                  0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on

@A.B. 谢谢你告诉我!现在很少有人给负评并留下评论,这需要勇气,也让我能够学到东西。已更新并适配了可用的版本。 - Ciro Santilli OurBigBook.com
在Precise版本上:http://packages.ubuntu.com/precise/chkconfig - Ciro Santilli OurBigBook.com

除了系统服务和脚本之外:

/etc/init.d/
/lib/systemd/system/
/etc/systemd/system/

可能还有一些自启动应用程序,例如:
find / -name "*autostart*"

ls -1 "/etc/xdg/autostart" "/home/$USER/.config/autostart" "/usr/share/gdm/autostart"  "/usr/share/gnome/autostart"

使用gawk:

ls -l /etc/rc*.d/* | gawk 'match($0, /rc([0-6S]).d.*\/(.*)$/, a) {l[a[2]]=l[a[2]]a[1]","}; END{for(v in l){print v,substr(l[v],1,length(l[v])-1)}}'

示例输出:
$ ls -l /etc/rc*.d/* | gawk 'match($0, /rc([0-6S]).d.*\/(.*)$/, a) {l[a[2]]=l[a[2]]a[1]","}; END{for(v in l){print v,substr(l[v],1,length(l[v])-1)}}' | egrep README
README 0,1,2,3,4,5,6,S