Systemd systemctl 列出 Linux 上所有失败的单元/服务
本快速教程解释如何使用 systemctl 命令在 Linux 操作系统上查找/列出所有失败的 systemd 服务/单元。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 是的 |
要求 | Linux 和 systemd |
预计阅读时间 | 4 分钟 |
Systemd systemctl 列出所有失败的单元/服务命令
systemctl 命令用于控制 systemd 系统和服务管理。要列出 systemd 当前在内存中的单元,请在 Linux 上将 list-units 参数传递给 systemctl,如下所示:
sudo systemctl list-units --failed
## OR ##
sudo systemctl list-units --state failed
systemctl 命令选项列出所有失败的单元/服务
关于 is-failed 选项的说明
我们可以检查 Linux 下是否有任何指定的单元或服务处于“失败”状态。如果至少有一个单元或服务失败,则以下命令返回退出代码 0,否则返回非零值。除非--quiet传递给 systemctl 命令的选项,否则这还会将当前单元状态打印到标准输出。因此,如果您知道服务或单元名称,请尝试以下命令:
另一个选项是运行以下简单命令以及grep 命令,以使用 systemctl 列出失败的服务:
sudo systemctl is-failed {service-name-here}
sudo systemctl is-failed nginx.service
sudo systemctl is-failed vboxweb.service
sudo systemctl list-units | grep -i failed
如何查找 systemd 单元或服务的状态
传递status选项如下:
sudo systemctl status {service-name}
sudo systemctl status ssh.service
sudo systemctl status nginx.service
sudo systemctl status vboxweb.service
显示服务单元的属性
运行以下命令:
sudo systemctl show {service-name}
sudo systemctl show vboxweb.service
如何查看日志消息
日志消息可以提供有关服务的详细信息,我们可以使用这些信息来调试问题。语法如下:
显示所有消息
journalctl
journalctl | grep 'error'
仅显示内核消息
journalctl -k
在屏幕上实时查看消息
journalctl -f
如何仅查看服务特定的消息
journalctl -u {service-name-here}
journalctl -u ssh.service
journalctl -u vboxweb.service
-- Reboot -- Apr 17 00:41:06 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: WARNING: The character device /dev/vboxdrv does not exist. Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: Please install the virtualbox-dkms package and the appropriate Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: headers, most likely linux-headers-generic. Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: You will not be able to start VMs until this problem is fixed. Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: Oracle VM VirtualBox web service Version 6.0.14_Ubuntu Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: (C) 2007-2019 Oracle Corporation Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: All rights reserved. Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001759 main VirtualBox web service 6.0.14_Ubuntu r132055 linux.amd64 (Oct 17 2019 07:24:16) release log Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001764 main Log opened 2020-04-16T19:11:06.826028000Z Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001765 main Build Type: release Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001768 main OS Product: Linux Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001769 main OS Release: 5.3.0-46-generic Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001770 main OS Version: #38-Ubuntu SMP Fri Mar 27 17:37:05 UTC 2020 Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001831 main DMI Product Name: 20QVCTO1WW Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001855 main DMI Product Version: ThinkPad X1 Extreme 2nd Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001920 main Host RAM: 31866MB (31.1GB) total, 31409MB (30.6GB) available Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001924 main Executable: /usr/lib/virtualbox/vboxwebsrv Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001924 main Process ID: 1675 Apr 17 00:41:06 example-wks01 vboxwebsrv[1675]: 00:00:00.001925 main Package type: LINUX_64BITS_GENERIC (OSE) Apr 17 00:41:06 example-wks01 systemd[1]: Started VirtualBox Web Service. Apr 20 11:44:00 example-wks01 systemd[1]: Stopping VirtualBox Web Service... Apr 20 11:44:05 example-wks01 systemd[1]: vboxweb.service: Succeeded. Apr 20 11:44:05 example-wks01 systemd[1]: Stopped VirtualBox Web Service. Apr 20 11:44:05 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 20 11:44:05 example-wks01 systemd[1]: vboxweb.service: Can't open PID file /run/vboxweb.pid (yet?) after start: No such file or directory Apr 20 11:44:05 example-wks01 systemd[1]: vboxweb.service: Failed with result 'protocol'. Apr 20 11:44:05 example-wks01 systemd[1]: Failed to start VirtualBox Web Service. -- Reboot -- Apr 20 11:52:48 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 20 11:52:48 example-wks01 systemd[1]: vboxweb.service: Can't open PID file /run/vboxweb.pid (yet?) after start: Operation not permitted Apr 20 11:52:48 example-wks01 systemd[1]: vboxweb.service: Failed with result 'protocol'. Apr 20 11:52:48 example-wks01 systemd[1]: Failed to start VirtualBox Web Service. -- Reboot -- Apr 20 12:17:31 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 20 12:17:31 example-wks01 systemd[1]: vboxweb.service: Can't open PID file /run/vboxweb.pid (yet?) after start: Operation not permitted Apr 20 12:17:31 example-wks01 systemd[1]: vboxweb.service: Failed with result 'protocol'. Apr 20 12:17:31 example-wks01 systemd[1]: Failed to start VirtualBox Web Service. -- Reboot -- Apr 20 12:35:06 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 20 12:35:06 example-wks01 systemd[1]: vboxweb.service: Can't open PID file /run/vboxweb.pid (yet?) after start: Operation not permitted Apr 20 12:35:06 example-wks01 systemd[1]: vboxweb.service: Failed with result 'protocol'. Apr 20 12:35:06 example-wks01 systemd[1]: Failed to start VirtualBox Web Service. -- Reboot -- Apr 21 00:09:56 example-wks01 systemd[1]: Starting VirtualBox Web Service... Apr 21 00:09:56 example-wks01 systemd[1]: vboxweb.service: Can't open PID file /run/vboxweb.pid (yet?) after start: Operation not permitted
我们现在可以在线搜索这些错误或阅读手册页来解决问题。
结论
我们学习了如何使用 systemctl 命令显示在 Linux 上运行失败的服务/单元。有关更多信息,请参阅此处的 systemctl 手册页。