新聞中心
Linux 服務(wù)管理兩種方式service和systemctl

systemd是Linux系統(tǒng)最新的初始化系統(tǒng)(init),作用是提高系統(tǒng)的啟動(dòng)速度,盡可能啟動(dòng)較少的進(jìn)程,盡可能更多進(jìn)程并發(fā)啟動(dòng)。
systemd對(duì)應(yīng)的進(jìn)程管理命令是systemctl
列出啟動(dòng)失敗的服務(wù)
[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'.
可以發(fā)現(xiàn),有個(gè)一個(gè)服務(wù)啟動(dòng)失敗了。
is-failed選項(xiàng)
可以使用is-failed選項(xiàng)檢查指定的服務(wù)是否啟動(dòng)失敗。如果啟動(dòng)失敗,結(jié)果是failed。如果啟動(dòng)沒有問題,結(jié)果是active。
[root@localhost ~]# systemctl is-failed httpd
failed
[root@localhost ~]# systemctl is-failed vsftpd
active
檢查服務(wù)的狀態(tài)
可以使用status選項(xiàng),查看服務(wù)啟動(dòng)失敗的原因,下面狀態(tài)信息里面告訴我們,是httpd.conf配置文件354行有語(yǔ)法錯(cuò)誤。
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列出啟動(dòng)失敗的服務(wù)CentOS7中使用systemctl列出啟動(dòng)失敗的服務(wù)
使用journalctl查看服務(wù)的啟動(dòng)日志
如果使用systemctl status [unit]沒有找到服務(wù)啟動(dòng)失敗的原因,可以使用journalctl查看更多的啟動(dòng)日志。
下面操作是過濾出所有帶有error的行,可以找到啟動(dòng)失敗的服務(wù)。
[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.
也可以使用journalctl -u [unit]只查看某一個(gè)服務(wù)的啟動(dòng)日志:
[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.
#過濾出有錯(cuò)誤的信息。
[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.
發(fā)現(xiàn)/etc/httpd/conf/httpd.conf配置文件的354行,Directory標(biāo)簽沒有關(guān)閉,返現(xiàn)原因了,就馬上修改吧。
分享題目:通過systemctl列出啟動(dòng)失敗的服務(wù)
標(biāo)題URL:http://www.dlmjj.cn/article/ccdjhco.html


咨詢
建站咨詢
