1

I am new to Linux and experimenting with scheduling a task on a Raspberry Pi B. I can not get the scheduled task to run nor do I see the scheduled task when I use crontab -l. When I run crontab -l I get the message no crontab for pi.

I have opened cron using the command

sudo crontab -e

I want to schedule a python script to run every minute so I have this line in the crontab file:

* * * * * /home/pi/range_sensor.py

My python file's first line contains #!/usr/bin/env python and I have used chmod +x /home/pi/range_sensor.py to change the execute permissions. Why is my scheduled task not running every minute? Where did I go wrong?

2
  • How do you know it is not running? How would you confirm if it were running?
    – SiKing
    Commented Dec 15, 2016 at 17:26
  • Maybe you must use * * * * * pi "python /home/pi/range_sensor.py" in the case of it should be launched as "pi" user. Did you restarted the daemon after doing changes? Commented Dec 15, 2016 at 17:36

2 Answers 2

3

Your first issue is with understanding sudo.

Your command crontab -l shows you scheduled crons for current user: "pi" according to the error message.

Your second command sudo crontab -e says: as the super user (root) edit the crons.

So sudo crontab -l should show you crons for the super user.

Second part: Cron does not run under the X.org server (a graphical environment) therefore it cannot know the environmental variable necessary to be able to start an X.org server application so they will have to be defined.

You can Google for various solutions. One very good explanation is available on the ArchLinux wiki.

2
  • Yes, thanks sudo crontab -l shows the file, thanks. The python script has some print statements so I would expect to see an IDLE window open (or new terminal window) and display those print statements. Why do I not see a new terminal window pop open every minute? Actually, the sensor is not connected to the pi so the error handler in the script ought to return and print the error, yes?
    – GBG
    Commented Dec 15, 2016 at 17:48
  • @GBG see update in my answer.
    – SiKing
    Commented Dec 15, 2016 at 17:56
0

Crontab output has to go somewhere. On some systems, it's wrapped in an e-mail. On the Raspberry Pi, root crontab output seems to be quietly dropped. So if you want to log something, change the crontab line to

* * * * * /home/pi/range_sensor.py >> /home/pi/range_sensor-output.txt

This will trickle out new readings every minute. I might suggest printing a timestamp in your Python output so you can track what's changing.

(I'd also invite you to research whether you have to use sudo at all. Many GPIO libraries for Raspberry Pi no longer need root access, and can be run as a regular user. If you're following a tutorial that's from before mid-2015 or so, it may be suggesting sudo when you really should no longer use it.)

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.