Journalctl.用户单位的输出消失

And*_*sov 5 systemd

我有一个非常简单的SystemD用户单元:

~/.config/systemd/user/logtest.service

[Unit]
Description=log test
After=network.target

[Service]
Type=oneshot
ExecStart=/home/andrey/tmp/1.sh

[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)

1.sh 只是 echo

#!/bin/bash
echo "123"
Run Code Online (Sandbox Code Playgroud)

我启动它systemctl start --user logtest.service
然后我检查日志journalctl --user-unit logtest -n5但我123在这个输出中看不到我.为什么????

journalctl  --user-unit  logtest -n5                                                                          
-- Logs begin at ?? 2016-10-15 22:17:53 +07, end at ?? 2017-01-06 16:03:16 +07. --
??? 06 16:03:15 andrcomp systemd[1524]: Started log test.
??? 06 16:03:16 andrcomp systemd[1524]: Starting log test...
??? 06 16:03:16 andrcomp systemd[1524]: Started log test.
??? 06 16:03:16 andrcomp systemd[1524]: Starting log test...
??? 06 16:03:16 andrcomp systemd[1524]: Started log test.
Run Code Online (Sandbox Code Playgroud)

但如果我添加sleep 11.sh:

#!/bin/bash
echo "123"
sleep 1
Run Code Online (Sandbox Code Playgroud)

然后123出现在我的日志中

$ journalctl  --user-unit  logtest -n5                                                                          
-- Logs begin at ?? 2016-10-15 22:17:53 +07, end at ?? 2017-01-06 16:10:25 +07. --
??? 06 16:07:57 andrcomp systemd[1524]: Starting log test...
??? 06 16:07:57 andrcomp systemd[1524]: Started log test.
??? 06 16:10:24 andrcomp systemd[1524]: Starting log test...
??? 06 16:10:24 andrcomp 1.sh[3760]: 123
??? 06 16:10:25 andrcomp systemd[1524]: Started log test.
Run Code Online (Sandbox Code Playgroud)

为什么?

如果我检查所有用户登录,然后123在这里(不含sleep1.sh)

$ journalctl --user -n5                                                                                         
-- Logs begin at ?? 2016-12-18 16:15:56 +07, end at ?? 2017-01-06 16:13:32 +07. --
??? 06 16:13:32 andrcomp 1.sh[3997]: 123
??? 06 16:13:32 andrcomp systemd[1524]: Started log test.
??? 06 16:13:32 andrcomp systemd[1524]: Starting log test...
??? 06 16:13:32 andrcomp 1.sh[4007]: 123
??? 06 16:13:32 andrcomp systemd[1524]: Started log test.
Run Code Online (Sandbox Code Playgroud)

但我只需要查看某些单位日志

它作为系统单元工作正常(/usr/lib/systemd/system/logtest_syst.service)

$ journalctl -u logtest_syst                                                                                    
-- Logs begin at ?? 2016-10-15 22:17:53 +07, end at ?? 2017-01-06 15:43:59 +07. --
??? 06 15:43:59 andrcomp systemd[1]: Starting log test...
??? 06 15:43:59 andrcomp 1.sh[1082]: 123
??? 06 15:43:59 andrcomp systemd[1]: Started log test.
Run Code Online (Sandbox Code Playgroud)

什么是睡觉的魔力?或者可能是检查用户单位日志的正确方法?

Ort*_*kni 4

有一个错误标记为“CLOSED CANTFIX”:Journalctl miss to show logs from unit

红帽高级软件工程师 Michal Sekletar 表示:

这是一个已知的问题。systemd-journald 有时无法记录有关日志消息来源的单元的信息。如果记录消息的进程寿命较短,则遇到此问题的可能性较高。

[...]

日志不会在任何地方消失。只是对于一些源自短期进程的日志消息,journald 无法找出相应的 cgroup(单元)。因此,如果您根据单位名称应用过滤,您将不会看到这些日志行。在内核为我们提供一种以非活泼方式收集 cgroup 信息的方法之前,我们对此无能为力。但话又说回来,日志就在那里,并且您使用基于时间的过滤的第一个命令证明了这一点。

因此,您应该能够使用以下命令查看脚本的输出(没有睡眠的输出):

journalctl --since "2016-10-15 22:17:53"
Run Code Online (Sandbox Code Playgroud)

  • 令人遗憾的是,启动时失败的进程(因此导致我想用“journalctl”来调查它们)_准确地说_是那些短暂的进程! (2认同)