Ulf*_*lfR 6 ubuntu systemd journald
当我编写单元文件时,我希望Exec*
-options ( ExecStartPre=, ExecStartPost=, ExecStart=, and ExecStopPost=
) 的所有输出都发送到日志。但显然输出以某种方式缓冲,因此无法确定将写入日志的内容。
在我的单元文件 ( ulftest.service
) 中,我有以下部分:
...
ExecStartPre=/bin/echo 'Hello'
ExecStartPre=/usr/bin/who
ExecStart=/storage/_test/venv/bin/python /storage/_test/ulftestservice.py
...
Run Code Online (Sandbox Code Playgroud)
因此,除了正常的“开始”、“已开始”、“停止”和“已停止”行之外,我希望日志中会显示一个已登录用户的列表(我有 4 个用户)和一个“ Hello” 。
不是这种情况。如果它们出现或不出现,这完全是随机的(至少对我来说)。
我编写了一个小测试循环,每 2 秒重新启动一次服务:
$ for x in `seq 100`; do echo $x; sudo systemctl restart ulftest.service ; sleep 2; done
Run Code Online (Sandbox Code Playgroud)
运行之后我在日志中得到了这个:
...
2017-05-10T09:40:36+0000 ulf44 systemd[1]: Started UlfTest, Administrative interface.
2017-05-10T09:40:38+0000 ulf44 systemd[1]: Stopping UlfTest, Administrative interface...
2017-05-10T09:40:38+0000 ulf44 systemd[1]: Stopped UlfTest, Administrative interface.
2017-05-10T09:40:38+0000 ulf44 systemd[1]: Starting UlfTest, Administrative interface...
2017-05-10T09:40:38+0000 ulf44 who[27344]: ulf pts/0 2017-05-04 09:07 (192.168.0.180)
2017-05-10T09:40:38+0000 ulf44 who[27344]: ulf pts/1 2017-05-04 12:36 (192.168.0.180)
2017-05-10T09:40:38+0000 ulf44 who[27344]: ulf pts/2 2017-05-05 06:48 (192.168.0.180)
2017-05-10T09:40:38+0000 ulf44 systemd[1]: Started UlfTest, Administrative interface.
2017-05-10T09:40:40+0000 ulf44 systemd[1]: Stopping UlfTest, Administrative interface...
2017-05-10T09:40:40+0000 ulf44 systemd[1]: Stopped UlfTest, Administrative interface.
2017-05-10T09:40:40+0000 ulf44 systemd[1]: Starting UlfTest, Administrative interface...
2017-05-10T09:40:40+0000 ulf44 systemd[1]: Started UlfTest, Administrative interface.
...
2017-05-10T09:42:00+0000 ulf44 systemd[1]: Stopping UlfTest, Administrative interface...
2017-05-10T09:42:00+0000 ulf44 systemd[1]: Stopped UlfTest, Administrative interface.
2017-05-10T09:42:00+0000 ulf44 systemd[1]: Starting UlfTest, Administrative interface...
2017-05-10T09:42:00+0000 ulf44 who[28161]: ulf pts/0 2017-05-04 09:07 (192.168.0.180)
2017-05-10T09:42:00+0000 ulf44 who[28161]: ulf pts/1 2017-05-04 12:36 (192.168.0.180)
2017-05-10T09:42:00+0000 ulf44 who[28161]: ulf pts/2 2017-05-05 06:48 (192.168.0.180)
2017-05-10T09:42:00+0000 ulf44 who[28161]: ulf pts/3 2017-05-05 11:44 (192.168.0.180)
2017-05-10T09:42:00+0000 ulf44 systemd[1]: Started UlfTest, Administrative interface.
2017-05-10T09:42:02+0000 ulf44 systemd[1]: Stopping UlfTest, Administrative interface...
2017-05-10T09:42:02+0000 ulf44 systemd[1]: Stopped UlfTest, Administrative interface.
2017-05-10T09:42:02+0000 ulf44 systemd[1]: Starting UlfTest, Administrative interface...
2017-05-10T09:42:02+0000 ulf44 systemd[1]: Started UlfTest, Administrative interface.
...
Run Code Online (Sandbox Code Playgroud)
请注意,对于 100 次重新启动,我在大多数情况下没有得到任何输出,我没有收到Hello,但我得到了 2 轮输出who
,但只完成了其中一轮。
我使用的是新的 Ubuntu 16.04。我还在stackoverflow上提出了类似且相关的问题。
这是一个功能、一个错误还是只是我完全以错误的方式处理的东西?请建议我!
man systemd.exec
Run Code Online (Sandbox Code Playgroud)
我认为你需要尝试在你的单位使用StandardOutput=journal
journal connects standard output with the journal which is accessible via journalctl(1). Note that everything that is written to syslog or kmsg (see below) is
implicitly stored in the journal as well, the specific two options listed below are hence supersets of this one.
Run Code Online (Sandbox Code Playgroud)