Issue
I am on a raspberry pi 5 model B rev 1.0, and am trying to redirect the contents of this simple python program to a file:
import time
import sys
while True:
time.sleep(1)
sys.stdout.write("out\n")
sys.stderr.write("err\n")
I then added this run statement via sudo crontab -e
:
@reboot /usr/bin/python3 /home/pi/test.py >> /home/pi/cronlog.log
However, after restarting my computer, the only logs that I see in cronlog.log are the "err" logs.
I also tried this variation:
@reboot /usr/bin/python3 /home/pi/test.py >> /home/pi/cronlog.log 2>&1
and also even tried specifying the log file specifically:
@reboot /usr/bin/python3 /home/pi/test.py >> /home/pi/cronlog.log 2>> /home/pi/cronerr.log
Neither worked, only printing out the "err" message into the cronlog.log file.
As a last resort, I tried:
@reboot /bin/bash -c "/usr/bin/python3 /home/pi/test.py >> /home/py/cronlog.log"
And... it still only printed out the err logs.
Any help? Thanks!
As a note, I have tried all of these commands in my normal bash instance, and they work perfectly as expected.
Solution
Thank you to @larsks for providing this solution.
The issue was fixed I added a -u after the python3 command. (for unbuffered)
python3 -u test.py > file.txt
Answered By - unfestive chicken Answer Checked By - David Goodson (WPSolving Volunteer)