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)
2 Answers 2
To log any output:
sleep 60
python /home/pi/MyProgram/MyProgram.py >>/home/pi/MyProgram/log.txt 2>&1
Perhaps:
ZZZ=""
while [ -z "$ZZZ" ]
do
sleep 5
ZZZ="$(ps -ef | grep mqtt)"
done
python /home/pi/MyProgram/MyProgram.py >>/home/pi/MyProgram/log.txt 2>&1
if your script needs "mqtt" running
-
Now with the help of the log I could figure out, that the output of the method
os.getcwd()
was/root
and not/home/pi
like it was when called directly from the terminal. I corrected the script with hard coded references. So now the sript sometimes starts at boot and works. But not at every boot :( Is it possible that crontab isn't reliable for this purpose?sporc– sporc2018年04月20日 20:55:45 +00:00Commented Apr 20, 2018 at 20:55 -
Crontab is much more reliable than systemd. See my other comment.Gerard H. Pille– Gerard H. Pille2018年04月20日 21:57:06 +00:00Commented Apr 20, 2018 at 21:57
-
I have read the comment. Maybe this explains why it sometimes works and sometimes not with crontab.I dont understand what you mean with
put sleep and python in a script
sporc– sporc2018年04月20日 22:04:36 +00:00Commented Apr 20, 2018 at 22:04 -
A shell script?Gerard H. Pille– Gerard H. Pille2018年04月20日 22:57:38 +00:00Commented Apr 20, 2018 at 22:57
-
So telling cronjob to start the shell and inside the shell tell to sleep and then executing the python script?sporc– sporc2018年04月20日 23:05:48 +00:00Commented Apr 20, 2018 at 23:05
I think you need a full path to Python... try /usr/bin/python
, or python3 if that's what you need.
-
This just gives
sudo: usr/bin/python: command not found
. But with the answer from Gerard I could get the script working (sometimes). By now it's not very reliable.sporc– sporc2018年04月20日 20:57:06 +00:00Commented Apr 20, 2018 at 20:57 -
He meant /usr/bin/python, of course.Gerard H. Pille– Gerard H. Pille2018年04月20日 21:58:40 +00:00Commented Apr 20, 2018 at 21:58
-
@
Gerard H. Pille
is correct. Apologies for the stupid mistake :\Seamus– Seamus2018年04月21日 11:52:33 +00:00Commented Apr 21, 2018 at 11:52 -
cron
is looking in/usr/bin
and/bin
by default.Dmitry Grigoryev– Dmitry Grigoryev2018年04月30日 08:48:08 +00:00Commented Apr 30, 2018 at 8:48 -
sleep 60
like with crontab?