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:
/home/pi/MyProgram/MyProgram.py
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.
The logfile 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.
NOTE2: journalctl
gives the following lines about cron
:
cron[428]: (CRON) INFO (pidfile fd = 3)
...
cron[428]: (CRON) INFO (Running @reboot jobs)
CRON[451]: pam_unix(cron:session): session opened for user root by (uid=0)
...
CRON[464]: (root) CMD (sleep 60 && sudo python /home/pi/MyProgram/MyProgram.py 2>&1 >>/home/pi/MyProgram/log.txt)
sleep 60
like with crontab?