当我编写我的单元文件时,我希望将所有来自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 ...
所以我希望有一个login用户列表(我4)和在正常的开始,开始,停止和停止的行之外的杂志上出现一个你好 。
情况并非如此。 如果他们出现或不出现,这是完全(至less对我来说)随机的。
我写了一个小testing程序,每2秒重新启动一次服务:
$ for x in `seq 100`; do echo $x; sudo systemctl restart ulftest.service ; sleep 2; done
运行后,我在日记中得到这个:
... 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. ...
请注意,对于100次重新启动,大部分时间我都没有得到输出,但是我得到了来自who两轮输出who但是只有其中一个完成了。
我在一个新的Ubuntu 16.04。 我也问了一个类似的和相关的问题在stackoverflow 。
这是一个function,一个错误或只是我正在接近错误的方式吗? 请build议我!
man systemd.exec
我认为你需要尝试使用你的单位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.