CentOS7中使用systemctl列出啓動失敗的服務

本教程介紹如何使用systemctl工具列出啓動失敗的systemd管理的各個服務
列出啓動失敗的服務
[root@localhost ~]# systemctl list-units --state failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● httpd.service loaded failed failed The Apache HTTP Serverapache

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列出啓動失敗的服務code

檢查服務的狀態
可使用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 agoip

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

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

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列出啓動失敗的服務io

使用journalctl查看服務的啓動日誌
若是使用systemctl status [unit]沒有找到服務啓動失敗的緣由,可使用journalctl查看更多的啓動日誌。ftp

下面操做是過濾出全部帶有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手冊。

相關文章
相關標籤/搜索