
本教程介绍如何使用systemctl工具列出启动失败的systemd管理的各个服务。文内含长段代码可复制可往左滑,希望对大家有帮助! # s' H! c- \) G* x: Q* @+ D9 Q 列出启动失败的服务 [root@localhost ~]# systemctl list-units --state failed! d7 z- |6 E$ _; z" i$ O! LUNIT LOAD ACTIVE SUB DESCRIPTION ● httpd.service loaded failed failed The Apache HTTP Server 7 ]/ h" K& h- | LOAD = Reflects whether the unit definition was properly loaded.1 K/ |, C* K+ A: ~6 P 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'. ![]() 可以发现,有个一个服务启动失败了。
is-failed选项 可以使用is-failed选项检查指定的服务是否启动失败。如果启动失败,结果是failed。如果启动没有问题,结果是active。 failed6 W* K* _5 U# Y! m. T [root@localhost ~]# systemctl is-failed vsftpd# F2 I) P6 m) A* a- R active 7 M( ?" j7 I: e% C 检查服务的状态 ' h! ?4 a7 J% V4 d% C) n& o# t 可以使用status选项,查看服务启动失败的原因,下面状态信息里面告诉我们,是httpd.conf配置文件354行有语法错误。 [root@localhost ~]# systemctl status httpd ● httpd.service - The Apache HTTP Server8 t+ u& f% s" S- M' `4 H$ B Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)' ^% l8 |/ R6 q3 G+ i Active: failed (Result: exit-code) since Tue 2020-05-26 09:22:05 CST; 18min ago Docs: man:httpd(8)6 Z6 l2 i0 t" c j& P man:apachectl(8) Main PID: 2958 (code=exited, status=1/FAILURE) 0 E0 C+ w" q) m. M4 t May 26 09:22:05 localhost systemd[1]: Starting The Apache HTTP Server...6 h4 m& N* A& E$ U( ?8 m 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 i N! e8 N9 J B 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.: ?8 Y/ f6 M5 Z a Hint: Some lines were ellipsized, use -l to show in full.
% w* Y* }& O' X% h& Q" k 使用journalctl查看服务的启动日志 如果使用systemctl status [unit]没有找到服务启动失败的原因,可以使用journalctl查看更多的启动日志。 下面操作是过滤出所有带有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. N: e3 K" {5 ^ : v9 ~, z& t: S3 T5 U. [! @ ![]() 0 o, q4 d) _+ n J$ c1 M 也可以使用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..., C- P+ D* |8 z0 i! ]9 Q% g) L 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.* [" N1 }' a3 S0 Q1 L May 26 09:22:05 localhost systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE, z1 Y1 f7 ~5 q& {" C. O May 26 09:22:05 localhost kill[2959]: kill: cannot find process "") N; T* F/ @3 c) V( g May 26 09:22:05 localhost systemd[1]: httpd.service: control process exited, code=exited status=12 }, p# `* x$ S1 n3 C May 26 09:22:05 localhost systemd[1]: Failed to start The Apache HTTP Server.8 y7 @+ t, m- H+ v6 ^) l- Z4 H" _ May 26 09:22:05 localhost systemd[1]: Unit httpd.service entered failed state.( d3 C |9 @3 b# O' N# M May 26 09:22:05 localhost systemd[1]: httpd.service failed. - n' m4 ]: J; d+ I1 k #过滤出有错误的信息。 [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. 4 [; P* W8 b# B2 }/ K7 ^ ![]() 发现 /etc/httpd/conf/httpd.conf配置文件的354行,Directory标签没有关闭,返现原因了,就马上修改吧。 ' U% V( J$ s+ ~4 x; X) |& r+ o 总 结 我们学习了如何使用systemctl命令显示在Linux上运行失败的服务/单元。有关详细信息,请查看systemctl手册。 |