I am trying to start a python script when the raspberry boots up. I have set up a new job in crontab, but it does not work liek expected. Here is what I did:
My script is in the following directory:
Then I made it executable by typing:
cd /home/pi/MyProgram sudo chmod +x MyProgram.py
Than I added a job to crontab by:
sudo crontab -e
I added this line:
@reboot sleep 60 && sudo python /home/pi/MyProgram/MyProgram.py 2>&1 >>/home/pi/MyProgram/log.txt
I need the 60 seconds sleep, cause of some services starting later, like mqtt.
After a reboot (and some seconds of waiting, because of the
sleep 60) the display gets black for a second, but than returns to the desktop. Getting black for a second is a sign for me that the script tries to start. The script initializes pygame as one of the first steps.
log.txt remains empty.
What is possibly wrong? Do I oversee something?
NOTE: Running the script in the terminal with
sudo python /home/pi/MyProgram/MyProgram.py works like a charm.
journalctl gives the following lines about
cron: (CRON) INFO (pidfile fd = 3) ... cron: (CRON) INFO (Running @reboot jobs) CRON: pam_unix(cron:session): session opened for user root by (uid=0) ... CRON: (root) CMD (sleep 60 && sudo python /home/pi/MyProgram/MyProgram.py 2>&1 >>/home/pi/MyProgram/log.txt)