如何查看 systemd 服务何时启动/停止/重新启动?

min*_*iot 17 systemd systemd-journald

我有一个服务(由我自己编写)在 Debian (Jessie) 服务器上运行,并且该服务自己的日志恰好表明它在特定时间重新启动。没有段错误或其他崩溃的迹象,所以我现在试图弄清楚应用程序是否以某种方式默默地失败并被 systemd 重新生成,或者用户是否有意通过systemctl.

shell 历史记录没有显示此类活动,但这并不是决定性的export HISTCONTROL=ignoreboth,因为 SSH 会话可能刚刚超时,阻止了以前登录的 bash 历史记录被写入磁盘。服务器当时没有重新启动。

但是我希望 systemd 本身应该保留一个日志,指示何时有意重新启动服务。令我惊讶的是,我找不到journalctl有关如何获取此类日志的任何文档(例如)。

其他一些帖子(例如,在哪里/为什么没有普通用户 systemd 服务的日志?)似乎表明应该有这样的日志消息:

Jan 15 19:28:08 qbd-x230-suse.site systemd[1]: Starting chatty.service...
Jan 15 19:28:08 qbd-x230-suse.site systemd[1]: Started chatty.service.
Run Code Online (Sandbox Code Playgroud)

但是我在我的系统上没有看到这样的日志消息。

有没有办法找出 systemd 服务何时启动、停止或重新启动?

编辑:似乎人们可能遇到的典型问题是他们journalctl以非特权用户身份运行。对我来说不是这种情况,我一直在运作root。在回复评论时,跑步grep systemd /var/log/syslog只给了我这个:

Jun  6 09:28:35 server systemd[22057]: Starting Paths.
Jun  6 09:28:35 server systemd[22057]: Reached target Paths.
Jun  6 09:28:35 server systemd[22057]: Starting Timers.
Jun  6 09:28:35 server systemd[22057]: Reached target Timers.
Jun  6 09:28:35 server systemd[22057]: Starting Sockets.
Jun  6 09:28:35 server systemd[22057]: Reached target Sockets.
Jun  6 09:28:35 server systemd[22057]: Starting Basic System.
Jun  6 09:28:35 server systemd[22057]: Reached target Basic System.
Jun  6 09:28:35 server systemd[22057]: Starting Default.
Jun  6 09:28:35 server systemd[22057]: Reached target Default.
Jun  6 09:28:35 server systemd[22057]: Startup finished in 59ms.
Jun  6 09:37:08 server systemd[1]: Reexecuting.
Run Code Online (Sandbox Code Playgroud)

jdf*_*jdf 16

如果您需要为此编写脚本,则应考虑使用该systemctl show 命令。对于脚本来说,它比尝试从status. 例如,要查找服务上次启动的时间,您可以使用:

$ systemctl show systemd-journald --property=ActiveEnterTimestamp
ActiveEnterTimestamp=Wed 2017-11-08 05:55:17 UTC
Run Code Online (Sandbox Code Playgroud)

如果您想查看所有可用的属性,只需省略该标志,它就会将它们全部转储出来。

$ systemctl show <service_name>
Run Code Online (Sandbox Code Playgroud)

可以在此处找到这些属性的文档。


Tom*_*rck 5

使用 Debian 上的默认配置,非特权用户将无法访问 systemd-journald 和 syslog 日志。如果以普通用户身份登录,您将收到来自journalctl的以下响应:

$ journalctl 
No journal files were found.
Run Code Online (Sandbox Code Playgroud)

这有点令人困惑。

如果您以 root 身份登录,journalctl --unit=yourservice应该会为您提供所需的信息。在我的服务器上之后systemctl restart bind9,我得到了这个journalctl --unit=bind9

Jun 03 18:20:24 ns systemd[1]: Stopping BIND Domain Name Server...
Jun 03 18:20:24 ns named[27605]: received control channel command 'stop'
Jun 03 18:20:24 ns systemd[1]: Starting BIND Domain Name Server...
Jun 03 18:20:24 ns systemd[1]: Started BIND Domain Name Server.
Run Code Online (Sandbox Code Playgroud)

如果我用 , 显式杀死bind9 kill -9journalctl --unit=bind9给出:

Jun 03 18:46:25 ns systemd[1]: bind9.service: main process exited, code=killed, status=9/KILL
Jun 03 18:46:25 ns rndc[28028]: rndc: connect failed: 127.0.0.1#953: connection refused
Jun 03 18:46:25 ns systemd[1]: bind9.service: control process exited, code=exited status=1
Jun 03 18:46:25 ns systemd[1]: Unit bind9.service entered failed state.
Jun 03 18:46:25 ns systemd[1]: bind9.service holdoff time over, scheduling restart.
Jun 03 18:46:25 ns systemd[1]: Stopping BIND Domain Name Server...
Jun 03 18:46:25 ns systemd[1]: Starting BIND Domain Name Server...
Jun 03 18:46:25 ns systemd[1]: Started BIND Domain Name Server.
Run Code Online (Sandbox Code Playgroud)

第一行表明进程因被杀死而死亡。

systemd-journald 还将所有日志消息转发到 syslog,因此您还应该在/var/log/syslog.

Systemd 和 systemd-journald 有一个默认编译配置,可以在/etc/systemd/system.conf和中更改/etc/systemd/journald.conf

了解默认情况下 systemd-journald 将日志存储在 下可能会很有用,/run因此tmpfs在重新启动后会消失。这意味着为了获取早于上次启动的日志消息,您必须查看系统日志文件。在这种情况下,journalctl 不会为您提供早于上次启动的日志。/etc/systemd/journald.conf这可以通过设置来更改Storage=persistent

记录这一点的手册页是:

man 8 systemd-journald
man 5 journald.conf
man 5 systemd-system.conf
man 5 systemd-user.conf
Run Code Online (Sandbox Code Playgroud)

另请注意,为了让 systemd 自动重新启动服务,必须在其.service文件中进行配置。从man 5 systemd.service

   Restart=
       Configures whether the service shall be
       restarted when the service process exits, is
       killed, or a timeout is reached. The service
       process may be the main service process, but it
       may also be one of the processes specified with
       ExecStartPre=, ExecStartPost=, ExecStop=,
       ExecStopPost=, or ExecReload=. When the death
       of the process is a result of systemd operation
       (e.g. service stop or restart), the service
       will not be restarted. Timeouts include missing
       the watchdog "keep-alive ping" deadline and a
       service start, reload, and stop operation
       timeouts.

       Takes one of no, on-success, on-failure,
       on-abnormal, on-watchdog, on-abort, or always.
       If set to no (the default), the service will
       not be restarted.
Run Code Online (Sandbox Code Playgroud)


kla*_*pez 3

您可以查看服务上次启动或重新启动的时间。使用service chatty statussystemctl status chatty. 以下是 apache2 或 httpd 服务的示例:

\n\n
# service apache2 status\n\xe2\x97\x8f apache2.service - LSB: Apache2 web server\n   Loaded: loaded (/etc/init.d/apache2)\n  Drop-In: /lib/systemd/system/apache2.service.d\n       \xe2\x94\x94\xe2\x94\x80forking.conf\n   Active: active (running) since ven. 2017-06-02 15:53:01 CEST; 21min ago\n  Process: 14773 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)\n  Process: 22912 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)\n  Process: 14880 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)\n   CGroup: /system.slice/apache2.service\n
Run Code Online (Sandbox Code Playgroud)\n\n

线Active: active (running) since Wen. 2017-06-02 15:53:01 CEST; 21min ago显示了服务如何运行,但我不知道您是否可以像“列表”一样显示您正在寻找的内容。

\n\n
# systemctl status httpd\n\n\xe2\x97\x8f httpd.service - The Apache HTTP Server\n   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)\n   Active: active (running) since Fri 2019-10-11 00:35:58 EEST; 1 weeks 3 days ago\n     Docs: man:httpd(8)\n           man:apachectl(8)\n  Process: 29728 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)\n Main PID: 10722 (httpd)\n   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"\n   Memory: 8.7M\n
Run Code Online (Sandbox Code Playgroud)\n