你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

如何使用systemctl命令显示在Linux上运行失败的服务/单元

[复制链接]
gaosmile 发布时间:2020-8-3 22:59
本教程介绍如何使用systemctl工具列出启动失败的systemd管理的各个服务。文内含长段代码可复制可往左滑,希望对大家有帮助!

5 D6 y& A5 q3 z" h. ]
列出启动失败的服务
[root@localhost ~]# systemctl list-units --state failed
3 x* `% |) L9 y2 K; I' Q3 k6 f  UNIT          LOAD   ACTIVE SUB    DESCRIPTION8 t8 H: l5 e, J% t+ x
● httpd.service loaded failed failed The Apache HTTP Server
3 w$ o, Z) s0 J2 h: H2 v0 N
5 u/ `, E8 O+ ALOAD   = Reflects whether the unit definition was properly loaded.
2 @* p- `6 F% P: K- J, |) M1 xACTIVE = The high-level unit activation state, i.e. generalization of SUB.
& x0 p+ W7 o8 K. d  ^+ ~7 zSUB    = The low-level unit activation state, values depend on unit type." k* f/ a3 v( ?7 B  o) E9 N, ]

5 `0 X+ |3 ]+ ?! b& T' ~, U9 O1 loaded units listed. Pass --all to see loaded but inactive units, too.
! B  O" N: ^$ Y4 }- A. BTo show all installed unit files use 'systemctl list-unit-files'.

! A5 F/ j/ @1 m$ W; \

7 j; G# ]5 [& q' g2 ]
微信图片_20200803225700.png

可以发现,有个一个服务启动失败了。

3 F) f+ z0 L2 ]% @+ N0 `: F2 M

is-failed选项

$ ]) V- e0 w; X0 T

可以使用is-failed选项检查指定的服务是否启动失败。如果启动失败,结果是failed。如果启动没有问题,结果是active。

* {! F0 ]9 A* E
[root@localhost ~]# systemctl is-failed httpd
0 ^. e+ y8 T) B" Hfailed
" y# W" A, w. O[root@localhost ~]# systemctl is-failed vsftpd
& k. E- X2 V( G0 r" h! q% l( @4 gactive
. D3 T( j6 G- X( Z- u

% U" ^2 |; d3 V) |2 l
检查服务的状态
, 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
' u6 b" f3 g" U1 I# B● httpd.service - The Apache HTTP Server
+ X, M( c8 W" m) A! N   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
2 |- T  W& P% A" c   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)
0 |  Z  f8 k$ ?9 y: c$ n Main PID: 2958 (code=exited, status=1/FAILURE)' z1 U" X  W% E# B

& ?- [/ ~! i; k3 C& O4 ]) VMay 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 ""
. e2 f6 a! Q9 kMay 26 09:22:05 localhost systemd[1]: httpd.service: control process exited, code=exited status=1
2 t1 O3 C3 p0 k, ^) ]6 }. k) I* [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.
1 J) n* \. |1 r/ Z' ~: cHint: Some lines were ellipsized, use -l to show in full.

& a& I: H4 g; L1 m; m& ^

* {' L0 ?/ D& u0 T( C2 n8 c# u5 ?

微信图片_20200803225712.png

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 X
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 J5 s# Z+ E2 m7 G1 l$ |
! o- q1 s; p/ E) E+ V. [# v  E* V
微信图片_20200803225903.png
2 E4 c* B7 A/ y7 F* x: y) `
也可以使用journalctl -u [unit]只查看某一个服务的启动日志:[root@localhost ~]# journalctl -u httpd.service
4 ?  ?3 c+ r5 A9 f-- 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.
+ |/ q2 ~" H) z! ]. BMay 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

' V) o, h! k- `; M* K8 |( q#过滤出有错误的信息。. 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.

& d2 z$ h2 N: k: \- I
5 j6 V4 \: \  r& M+ a" K
微信图片_20200803225919.jpg

发现 /etc/httpd/conf/httpd.conf配置文件的354行,Directory标签没有关闭,返现原因了,就马上修改吧。


5 W* A; I' L4 b  F/ p7 N: L

总    结

  C* o! O  P" ~4 q9 R" Q, Z

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

- V3 q8 `. O% T) ]* S! k
赞 收藏 评论0 发布时间:2020-8-3 22:59

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版