
Tengo la intención de registrar la salida de un script de Python. Aquí cómo se ve:
arm@stackoverflow > cat test.py
#!/usr/bin/python
from time import sleep
while True:
print("Hello")
sleep(1)
Intento utilizar la forma estándar de syslog para manejar los registros, por lo tanto intenté configurar JournalD y SystemD para enviar todo 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 utilizo el Rsyslog predeterminado de Debian para enrutar mis registros en algún archivo:
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
Pero cuando comienzo todo, todos los registros parecen estar obstruidos en alguna parte y se liberan una hora después del inicio (como un gran despool):
arm@stackoverflow > sudo systemctl start testservice.service && sleep 30
Entonces:
arm@stackoverflow > tail /var/log/testservice.log #nothing
Pruebas:
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
¿Sabes cómo obtener estos registros en tiempo real?
Varios. Este es un servidor de prueba en Raspberry Pi OS (ARM)
Respuesta1
Gracias a @meuh, agregar una bandera -u
al script de Python funcionó. Lo agregué a la línea shebang:
#!/usr/bin/python -u