![为什么 python-journald-rsyslog 似乎批量/延迟日志写入?](https://linux22.com/image/784116/%E4%B8%BA%E4%BB%80%E4%B9%88%20python-journald-rsyslog%20%E4%BC%BC%E4%B9%8E%E6%89%B9%E9%87%8F%2F%E5%BB%B6%E8%BF%9F%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%EF%BC%9F.png)
我打算记录一个 Python 脚本输出。它看起来像这样:
arm@stackoverflow > cat test.py
#!/usr/bin/python
from time import sleep
while True:
print("Hello")
sleep(1)
我尝试使用标准 syslog 方式来处理日志,因此我尝试配置 JournalD 和 SystemD 以如下方式发送所有内容Syslog
:
arm@stackoverflow > cat /etc/systemd/journald.conf | grep -vP "^#"
[Journal]
ForwardToSyslog=yes
arm@stackoverflow > cat /etc/systemd/system/testservice.service
[Unit]
Description="Test service"
StartLimitIntervalSec=500
StartLimitBurst=10
[Service]
Type=Simple
Restart=on-failure
RestartSec=5s
ExecStart=/home/pi/test.py
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=testservice
[Install]
WantedBy=multi-user.target
最后,我使用 Debian 的默认 Rsyslog 将我的日志路由到某个文件中:
arm@stackoverflow > tail -n1 /etc/rsyslog.conf
if $programname == 'testservice' then /var/log/testservice.log
arm@stackoverflow > sudo touch /var/log/testservice.log && sudo chmod a+rw /var/log
但是当我启动整个过程时,所有日志似乎都被堵塞在某处,并且在启动后一小时才被释放(就像一个大的 despool):
arm@stackoverflow > sudo systemctl start testservice.service && sleep 30
然后:
arm@stackoverflow > tail /var/log/testservice.log #nothing
测试:
arm@stackoverflow > sudo systemctl status testservice.service
● testservice.service - "Test service"
Loaded: loaded (/etc/systemd/system/testservice.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2023-02-21 09:20:06 CET; 1min 33s ago
arm@stackoverflow > ps aux | grep test
root 23488 0.0 0.6 13956 5968 ? Ss 09:20 0:00 /usr/bin/python /home/pi/test.py
您知道如何实时获取这些日志吗?
杂项。这是 Raspberry Pi OS (ARM) 上的测试服务器
答案1
感谢 @meuh,-u
在 python 脚本中添加标志就成功了。我将其添加到 shebang 行中:
#!/usr/bin/python -u