关于centos:CentOS7中使用systemctl列出启动失败的服务

36次阅读

共计 3431 个字符,预计需要花费 9 分钟才能阅读完成。

本教程介绍如何应用 systemctl 工具列出启动失败的 systemd 治理的各个服务
列出启动失败的服务
[root@localhost ~]# systemctl list-units –state failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● httpd.service loaded failed failed The Apache HTTP Server

LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass –all to see loaded but inactive units, too.
To show all installed unit files use ‘systemctl list-unit-files’.
CentOS7 中应用 systemctl 列出启动失败的服务 CentOS7 中应用 systemctl 列出启动失败的服务
能够发现,有个一个服务启动失败了。

is-failed 选项
能够应用 is-failed 选项查看指定的服务是否启动失败。如果启动失败,后果是 failed。如果启动没有问题,后果是 active。

[root@localhost ~]# systemctl is-failed httpd
failed
[root@localhost ~]# systemctl is-failed vsftpd
active
CentOS7 中应用 systemctl 列出启动失败的服务 CentOS7 中应用 systemctl 列出启动失败的服务

查看服务的状态
能够应用 status 选项,查看服务启动失败的起因,上面状态信息外面通知咱们,是 httpd.conf 配置文件 354 行有语法错误。

May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /…osed.
[root@localhost ~]# systemctl status httpd
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Tue 2020-05-26 09:22:05 CST; 18min ago

 Docs: man:httpd(8)
       man:apachectl(8)

Main PID: 2958 (code=exited, status=1/FAILURE)

May 26 09:22:05 localhost systemd[1]: Starting The Apache HTTP Server…
May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /…osed.
May 26 09:22:05 localhost systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
May 26 09:22:05 localhost kill[2959]: kill: cannot find process “”
May 26 09:22:05 localhost systemd[1]: httpd.service: control process exited, code=exited status=1
May 26 09:22:05 localhost systemd[1]: Failed to start The Apache HTTP Server.
May 26 09:22:05 localhost systemd[1]: Unit httpd.service entered failed state.
May 26 09:22:05 localhost systemd[1]: httpd.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
CentOS7 中应用 systemctl 列出启动失败的服务 CentOS7 中应用 systemctl 列出启动失败的服务

应用 journalctl 查看服务的启动日志
如果应用 systemctl status [unit]没有找到服务启动失败的起因,能够应用 journalctl 查看更多的启动日志。

上面操作是过滤出所有带有 error 的行,能够找到启动失败的服务。

[root@localhost ~]# journalctl |grep ‘error’
May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /etc/httpd/conf/httpd.conf:354: was not closed.
CentOS7 中应用 systemctl 列出启动失败的服务 CentOS7 中应用 systemctl 列出启动失败的服务
也能够应用 journalctl -u [unit]只查看某一个服务的启动日志:

[root@localhost ~]# journalctl -u httpd.service
— Logs begin at Sun 2020-05-24 06:52:52 CST, end at Tue 2020-05-26 09:48:03 CST. —
May 26 09:22:05 localhost systemd[1]: Starting The Apache HTTP Server…
May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /etc/httpd/conf/httpd.conf:354: was not closed.
May 26 09:22:05 localhost systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
May 26 09:22:05 localhost kill[2959]: kill: cannot find process “”
May 26 09:22:05 localhost systemd[1]: httpd.service: control process exited, code=exited status=1
May 26 09:22:05 localhost systemd[1]: Failed to start The Apache HTTP Server.
May 26 09:22:05 localhost systemd[1]: Unit httpd.service entered failed state.
May 26 09:22:05 localhost systemd[1]: httpd.service failed.

过滤出有谬误的信息。

[root@localhost ~]# journalctl -u httpd.service |grep ‘error’
May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /etc/httpd/conf/httpd.conf:354: was not closed.
CentOS7 中应用 systemctl 列出启动失败的服务 CentOS7 中应用 systemctl 列出启动失败的服务
发现 /etc/httpd/conf/httpd.conf 配置文件的 354 行,Directory 标签没有敞开,返现起因了,就马上批改吧。

总结
咱们学习了如何应用 systemctl 命令显示在 Linux 上运行失败的服务 / 单元。无关详细信息,请查看 systemctl 手册。

正文完
 0