如何询问 systemd 服务停止的原因

如何询问 systemd 服务停止的原因

我想知道为什么某个服务被停止。我能想到几个原因:

  • 服务崩溃
  • 服务自行退出
  • 服务已明确停止systemctl stop
  • 该服务已通过以下方式明确终止systemctl kill
  • 该服务是PartOf另一项已停止的服务
  • 该服务与已启动的另一个服务冲突。

我确信还有其他一些我忘记的。

有没有办法询问 systemd 是什么原因导致服务停止?

答案1

我会用:

要检查服务状态:

# systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
     Active: inactive (dead)
       Docs: man:httpd.service(8)

Feb 25 06:56:00 docker systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Feb 25 06:56:00 docker systemd[1]: httpd.service: Failed with result 'exit-code'.
Feb 25 06:56:00 docker systemd[1]: Failed to start The Apache HTTP Server.
Feb 25 06:56:32 docker systemd[1]: Starting The Apache HTTP Server...
Feb 25 06:56:32 docker httpd[18908]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5054:ff:fef5:a1f5%enp1s0. Set the 'ServerName' directive globally to suppress this message
Feb 25 06:56:33 docker httpd[18908]: Server configured, listening on: port 80
Feb 25 06:56:33 docker systemd[1]: Started The Apache HTTP Server.
Feb 25 07:02:15 docker systemd[1]: Stopping The Apache HTTP Server...
Feb 25 07:02:16 docker systemd[1]: httpd.service: Succeeded.
Feb 25 07:02:16 docker systemd[1]: Stopped The Apache HTTP Server.

检查服务日志(journalctl --unit=service_name):

# journalctl --unit=httpd
Feb 25 06:56:00 docker systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Feb 25 06:56:00 docker systemd[1]: httpd.service: Failed with result 'exit-code'.
Feb 25 06:56:00 docker systemd[1]: Failed to start The Apache HTTP Server.
Feb 25 06:56:32 docker systemd[1]: Starting The Apache HTTP Server...
Feb 25 06:56:32 docker httpd[18908]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5054:ff:fef5:a1f5%enp1s0. Set the 'ServerName' directive globally to suppress this message
Feb 25 06:56:33 docker httpd[18908]: Server configured, listening on: port 80
Feb 25 06:56:33 docker systemd[1]: Started The Apache HTTP Server.
Feb 25 07:02:15 docker systemd[1]: Stopping The Apache HTTP Server...
Feb 25 07:02:16 docker systemd[1]: httpd.service: Succeeded.
Feb 25 07:02:16 docker systemd[1]: Stopped The Apache HTTP Server.

答案2

您可以使用以下方式获取更多详细信息:

systemctl show <service_name>

相关内容