我有一个服务(由我自己编写)在 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.
但我在我的系统上没有看到此类日志消息。
有没有办法找出 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.
答案1
如果您需要编写脚本,您应该考虑使用该systemctl show
命令。对于脚本来说,它比尝试解析status
.例如,要查找服务上次启动的时间,您可以使用:
$ systemctl show systemd-journald --property=ActiveEnterTimestamp
ActiveEnterTimestamp=Wed 2017-11-08 05:55:17 UTC
如果您想查看所有可用的属性,只需省略该标志,它就会将它们全部转储出来。
$ systemctl show <service_name>
可以找到这些属性的文档这里。
答案2
使用 Debian 上的默认配置,非特权用户将无法访问 systemd-journald 和 syslog 日志。如果以普通用户身份登录,您将收到来自journalctl的以下响应:
$ journalctl
No journal files were found.
这有点令人困惑。
如果您以 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.
如果我用 , 显式杀死bind9 kill -9
,journalctl --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.
第一行表明进程因被杀死而死亡。
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
另请注意,为了让 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.
答案3
您可以查看服务上次启动或重新启动的时间。使用service chatty status
或systemctl status chatty
.以下是 apache2 或 httpd 服务的示例:
# service apache2 status
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2)
Drop-In: /lib/systemd/system/apache2.service.d
└─forking.conf
Active: active (running) since ven. 2017-06-02 15:53:01 CEST; 21min ago
Process: 14773 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
Process: 22912 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
Process: 14880 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/apache2.service
该行Active: active (running) since Wen. 2017-06-02 15:53:01 CEST; 21min ago
显示了服务如何运行,但我不知道您是否可以像“列表”一样显示您正在寻找的内容。
# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-10-11 00:35:58 EEST; 1 weeks 3 days ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 29728 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 10722 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
Memory: 8.7M