1

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 1

1

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
  • That worked! I didn't think that sort of thing would only affect what was being logged as cronjob, whereas normally when I ran it from cmd line, the output was logged. Thanks
    – Jacob
    Commented May 2, 2021 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .