Crontab Not Logging to File

Crontab Not Logging to File

I have the following cronjob:

@reboot /usr/local/bin/python3.9 /home/pi/push_bullet/push_bullet_socket.py >> /var/log/push_bullet_socket.py.log 2>&1

It's the only thing on my crontab. When I look into /var/log/push_bullet_socket.py.log I see that the file exists, but none of the stdout is being sent to file. The script works fine when it's not running as a cronjob, and other than the output not being logged, it works fine as a cronjob. I can confirm that the print statements are being ran in the python script, but for some reason they're not being output to the log file.

This is what's in cron.log after a reboot:

May  2 10:01:03 raspberrypi cron[351]: (CRON) INFO (pidfile fd = 3)
May  2 10:01:03 raspberrypi cron[351]: (CRON) INFO (Running @reboot jobs)
May  2 10:01:03 raspberrypi CRON[401]: (root) CMD (python /bin/push_bullet_socket.py >> /var/log/push_bullet_socket.py.log 2>&1)
May  2 10:01:03 raspberrypi CRON[402]: (pi) CMD (/usr/local/bin/python3.9 /home/pi/push_bullet/push_bullet_socket.py >> /var/log/push_bullet_socket.py.log 2>&1)

1 Answer

Python, like most other runtimes, buffers stdout when it is not written to a terminal. (Until 3.8 this applied to stderr as well.) Your writes are buffered until they accumulate 8 kB, or until you call sys.stdout.flush() or print(..., flush=True).

In 3.9 you have the option to change buffering mode (line buffering is what you normally get on terminal):

sys.stdout.reconfigure(line_buffering=True)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.