使用systemctl 命令查看启动服务
systemctl 是 CentOS 7 及以后版本中用于管理 systemd 系统和服务的命令,以下是一些常用的命令和示例:

1. 列出所有自启动的服务
要列出所有已启用(即开机自启动)的服务,可以使用以下命令:
systemctl listunitfiles type=service | grep enabled
该命令会显示所有已启用的服务及其状态。
nginx.service enabled apache2.service enabled
2. 列出所有服务及其启动状态
要查看所有服务以及它们的启动状态(包括已启用和已禁用的服务),可以使用:
systemctl listunitfiles type=service
输出示例:

20network.service static alsarestore.service disabled alsastate.service disabled alsastore.service disabled apache2.service enabled atd.service disabled auditd.service enabled ...
以enabled 结尾的表示这些服务是开机自启动的。
3. 查看特定服务的启动状态
要查看某个特定服务的启动状态,可以使用:
systemctl listunitfiles | grep 服务名称
要查看 Nginx 的启动状态:
systemctl listunitfiles | grep nginx
输出可能如下:
nginx.service enabled
这表示 Nginx 是开机自启动的。

4. 查看服务的运行状态
除了查看服务的启动状态外,还可以使用systemctl status 命令来查看服务的当前运行状态:
systemctl status 服务名.service
要查看 Nginx 的运行状态:
systemctl status nginx.service
输出示例:
● nginx.service The Nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 20230119 10:00:00 UTC; 2s ago
Docs: man:nginx(8)
Main PID: 1234 (nginx)
CGroup: /system.d/system/nginx.service
├─1234 nginx: master process /usr/sbin/nginx
└─1235 nginx: worker process这表明 Nginx 正在运行并且是开机自启动的。
使用chkconfig 命令查看启动服务(适用于 CentOS 6)
虽然 CentOS 7 及以后版本推荐使用systemctl,但在 CentOS 6 中仍然可以使用chkconfig 命令来管理服务:
1. 列出所有服务及其启动状态
要列出所有服务及其启动状态,可以使用:
chkconfig list
输出示例:
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off ...
on 表示服务在相应的运行级别下是启动的,off 表示不启动。
2. 查看特定服务的启动状态
要查看特定服务的启动状态,可以使用:
chkconfig list 服务名
要查看 Nginx 的启动状态:
chkconfig list nginx
输出可能如下:
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
这表示 Nginx 在运行级别 2、3、4、5 下是启动的。
| 命令 | 功能描述 | 适用版本 | 示例 |
|||||
|systemctl listunitfiles type=service | grep enabled | 列出所有开机自启动的服务 | CentOS 7+ |systemctl listunitfiles type=service | grep enabled |
|systemctl listunitfiles type=service | 列出所有服务及其启动状态 | CentOS 7+ |systemctl listunitfiles type=service |
|systemctl listunitfiles | grep 服务名称 | 查看特定服务的启动状态 | CentOS 7+ |systemctl listunitfiles | grep nginx |
|systemctl status 服务名.service | 查看服务的当前运行状态 | CentOS 7+ |systemctl status nginx.service |
|chkconfig list | 列出所有服务及其启动状态 | CentOS 6 |chkconfig list |
|chkconfig list 服务名 | 查看特定服务的启动状态 | CentOS 6 |chkconfig list nginx |
常见问题解答(FAQs)
Q1: 如何启用或禁用一个服务的开机自启动?
A: 使用systemctl enable 或systemctl disable 命令可以启用或禁用服务的开机自启动。
启用 Nginx 的开机自启动 systemctl enable nginx.service 禁用 Nginx 的开机自启动 systemctl disable nginx.service
对于 CentOS 6,可以使用chkconfig:
启用 Nginx 的开机自启动 chkconfig add nginx 禁用 Nginx 的开机自启动 chkconfig del nginx
Q2: 如何手动启动、停止或重启一个服务?
A: 使用systemctl start、systemctl stop、systemctl restart 命令可以控制服务的运行状态。
启动 Nginx systemctl start nginx.service 停止 Nginx systemctl stop nginx.service 重启 Nginx systemctl restart nginx.service
