1

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)
4
  • 1
    Using sudo like that will be tricky. Did you set it up to not ask for password? Might be better to run it from root's crontab. Also, did you consider using it as a systemd service instead
    – eftshift0
    Apr 20, 2018 at 16:13
  • off-topic ( raspberrypi.stackexchange.com/help/on-topic ), I would ask this at stackoverflow.com or unix.stackexchange.com
    – Fabian
    Apr 20, 2018 at 19:19
  • I tried with systemd, but I couldn't time the service correctly. Is there a possability to tell sleep 60 like with crontab?
    – sporc
    Apr 20, 2018 at 20:58
  • Of course: put sleep and python in a script on separate lines. As it is now, if the sleep is interrupted, python won't run. Apr 20, 2018 at 21:54

2 Answers 2

3

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

6
  • 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
    Apr 20, 2018 at 20:55
  • Crontab is much more reliable than systemd. See my other comment. 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
    Apr 20, 2018 at 22:04
  • A shell script? 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
    Apr 20, 2018 at 23:05
0

I think you need a full path to Python... try /usr/bin/python, or python3 if that's what you need.

5
  • 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
    Apr 20, 2018 at 20:57
  • He meant /usr/bin/python, of course. Apr 20, 2018 at 21:58
  • @Gerard H. Pille is correct. Apologies for the stupid mistake :\
    – Seamus
    Apr 21, 2018 at 11:52
  • cron is looking in /usr/bin and /bin by default. Apr 30, 2018 at 8:48
  • Maybe... I feel specifying full paths a good habit, but perhaps overkill in this case.
    – Seamus
    May 1, 2018 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.