|
本教程介绍如何使用systemctl工具列出启动失败的systemd管理的各个服务。文内含长段代码可复制可往左滑,希望对大家有帮助! 列出启动失败的服务 [root@localhost ~]# systemctl list-units --state failedUNIT LOAD ACTIVE SUB DESCRIPTION8 t8 H: l5 e, J% t+ x ● 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." k* f/ a3 v( ?7 B o) E9 N, ] 1 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
可以发现,有个一个服务启动失败了。 3 F) f+ z0 L2 ]% @+ N0 `: F2 M is-failed选项 可以使用is-failed选项检查指定的服务是否启动失败。如果启动失败,结果是failed。如果启动没有问题,结果是active。 * {! F0 ]9 A* E [root@localhost ~]# systemctl is-failed httpdfailed [root@localhost ~]# systemctl is-failed vsftpd active. D3 T( j6 G- X( Z- u 检查服务的状态 , O4 {# v2 V/ I4 n7 J2 i 可以使用status选项,查看服务启动失败的原因,下面状态信息里面告诉我们,是httpd.conf配置文件354行有语法错误。 2 r2 ?4 b6 k. h3 B9 m; c; Y May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /...osed.5 I" |* Q7 u" F( L8 ]) d[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% w* G" z% ]9 M/ P5 B Docs: man:httpd(8) D7 t( v, m9 z6 c) V+ B7 e1 I7 H, G man:apachectl(8) Main PID: 2958 (code=exited, status=1/FAILURE)' z1 U" X W% E# B May 26 09:22:05 localhost systemd[1]: Starting The Apache HTTP Server...5 k" P' A3 K- i" v May 26 09:22:05 localhost httpd[2958]: httpd: Syntax error on line 354 of /etc/httpd/conf/httpd.conf: /...osed.. S2 {5 A% C" X l; K May 26 09:22:05 localhost systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE, l& k2 l' M; q 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./ _6 S7 b/ w- m( g+ Y* _" @+ j May 26 09:22:05 localhost systemd[1]: Unit httpd.service entered failed state.8 o% a( Q; B% V+ }. w May 26 09:22:05 localhost systemd[1]: httpd.service failed. Hint: Some lines were ellipsized, use -l to show in full.
0 D8 ~! W0 l# ?7 o% y 使用journalctl查看服务的启动日志 如果使用systemctl status [unit]没有找到服务启动失败的原因,可以使用journalctl查看更多的启动日志。 下面操作是过滤出所有带有error的行,可以找到启动失败的服务。 + \, P& L' v( F [root@localhost ~]# journalctl |grep 'error'( h+ o8 n R! I2 [7 XMay 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 J5 s# Z+ E2 m7 G1 l$ | ! o- q1 s; p/ E) E+ V. [# v E* V
2 E4 c* B7 A/ y7 F* x: y) ` 也可以使用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. --5 ^9 N0 b, Q# t+ z May 26 09:22:05 localhost systemd[1]: Starting The Apache HTTP Server...4 n, t2 Z- c# d$ ?) H% k/ H& C/ D. { 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.1 r: `( y2 L. w0 o: T) k May 26 09:22:05 localhost systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE& R/ l& r7 l1 A0 Z, i9 c8 P May 26 09:22:05 localhost kill[2959]: kill: cannot find process ""- Q2 w+ }' A' R' J' h7 N# X }1 F7 M* } May 26 09:22:05 localhost systemd[1]: httpd.service: control process exited, code=exited status=1) E' k% I2 O) E0 k, h; E 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.! } ?! d* Q( [1 c- X) @ May 26 09:22:05 localhost systemd[1]: httpd.service failed., J8 R1 i: D9 |$ c9 _- Z( I$ z #过滤出有错误的信息。. i. n5 g! I& I* }% l' J w [root@localhost ~]# journalctl -u httpd.service |grep 'error'6 f r0 S# f, c- S7 \7 f* r 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. 5 j6 V4 \: \ r& M+ a" K
发现 /etc/httpd/conf/httpd.conf配置文件的354行,Directory标签没有关闭,返现原因了,就马上修改吧。
总 结 我们学习了如何使用systemctl命令显示在Linux上运行失败的服务/单元。有关详细信息,请查看systemctl手册。 - V3 q8 `. O% T) ]* S! k |
微信公众号
手机版