I have a Pi 4 with bookworm, running a python 3 script.
The python script generates logging output to stdout (to the terminal) when run from a prompt. I want to run the script from cron and capture the output to a file. These lines are part of a cron-run.sh
script started from cron.
#!/bin/bash MYLOGFILE=/home/pi/logs/python-log.txt python /home/pi/mypython.py -switch param1 param2 >> $MYLOGFILE
The log file, /home/pi/logs/python-log.txt
is created as a zero byte file, and remains zero bytes as the python script runs.
All paths are fully specified, which is the issue often mentioned with cron-related problems. The full path to python is not needed, since I can see it running when started from cron with `ps aux | grep "python"'
Why does redirection of python stdout to a file not work when started from cron?
cron runs a script which contains the lines above. crontab looks like this:
# m h dom mon dow command */30 * * * * /home/pi/cron-run.sh