0

I intend to log a python script output. Here what it looks like :

arm@stackoverflow > cat test.py
#!/usr/bin/python
from time import sleep
while True:
        print("Hello")
        sleep(1)

I try to use standard syslog way to handle logs, I therefore tried to configure JournalD & SystemD to send everything as 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

Finally I use Debian's default Rsyslog to route my logs in some file:

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

But when I start the whole, all logs seems to be clogged somewhere, and are releases at one hours after start (like a big despool):

arm@stackoverflow > sudo systemctl start testservice.service && sleep 30

Then:

arm@stackoverflow > tail /var/log/testservice.log #nothing

Tests:

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

Do you know how to get these logs in realtime?

Misc. This is a test server on Raspberry Pi OS (ARM)

moutonjr
  • 498
  • 5
  • 10
  • Please don't [cross post](https://serverfault.com/questions/1123389/why-python-journald-rsyslog-seems-to-batch-dely-log-write). As this obviously is not in a business environment I'm voting to close as off topic. – Gerald Schneider Feb 21 '23 at 08:54
  • 1
    As Python to run unbuffered stdout, either run `-u`, or set `PYTHONUNBUFFERED=1` in env. – meuh Feb 21 '23 at 13:56
  • @GeraldSchneider ... Indeed this is for business purposes. I just anonymized/simplified content. If it should be elsewhere, please tell. – moutonjr Feb 21 '23 at 19:05

1 Answers1

0

Thanks to @meuh, Adding flag -u to the python script did the trick. I added it to the shebang line:

#!/usr/bin/python -u
moutonjr
  • 498
  • 5
  • 10