Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1000906
  • 博文数量: 481
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5078
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-07 14:48
个人简介

分享工作和学习中的点点滴滴,包括前端、后端、运维、产品等各个方面,欢迎您来关注订阅!

文章分类

全部博文(481)

文章存档

2023年(26)

2022年(97)

2021年(119)

2020年(153)

2019年(70)

2018年(16)

我的朋友

分类: LINUX

2021-05-18 07:57:19

本教程介绍如何使用systemctl工具列出启动失败的systemd管理的各个服务
列出启动失败的服务
[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'.

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列出启动失败的服务

检查服务的状态

可以使用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 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列出启动失败的服务CentOS7中使用systemctl列出启动失败的服务

使用journalctl查看服务的启动日志

如果使用systemctl status [unit]没有找到服务启动失败的原因,可以使用journalctl查看更多的启动日志。

下面操作是过滤出所有带有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显示在上运行失败的服务/单元。有关详细信息,请查看systemctl手册。

阅读(946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~