
Pretendo registrar uma saída de script python. Aqui está o que parece:
arm@stackoverflow > cat test.py
#!/usr/bin/python
from time import sleep
while True:
print("Hello")
sleep(1)
Tento usar o modo syslog padrão para lidar com logs; portanto, tentei configurar o JournalD e o SystemD para enviar tudo como 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
Finalmente eu uso o Rsyslog padrão do Debian para rotear meus logs em algum arquivo:
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
Mas quando inicio tudo, todos os logs parecem estar obstruídos em algum lugar e são liberados uma hora após o início (como um grande despool):
arm@stackoverflow > sudo systemctl start testservice.service && sleep 30
Então:
arm@stackoverflow > tail /var/log/testservice.log #nothing
Testes:
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
Você sabe como obter esses logs em tempo real?
Diversos. Este é um servidor de teste no Raspberry Pi OS (ARM)
Responder1
Graças a @meuh, adicionar flag -u
ao script python resolveu o problema. Eu adicionei à linha shebang:
#!/usr/bin/python -u