5

I have a simple problem that I cannot seem to find an answer to even though I have done some research. I am trying to run a python script via crontab (I have never worked with crontab before this haha). Currently, this is what I have in my crontab -e

*/2 * * * * /home/pi/path_to_script/fileName.py

I want the script to be run every 2 minutes. The script 'fileName.py' writes to a text file so I know whether the crontab has run the script or not: the text file is never changed. I also have some print() lines in the script and those messages are never outputted. Furthermore, the first line in fileName.py is

#!/usr/bin/python3

So I believe I got the shebang directory down. I also tried the following in the crontab -e

*/2 * * * * /usr/bin/python3 /home/pi/path_to_script/fileName.py

and that does not seem to work. Also I have complied fileName.py with chmod +x so the script runs when I type ./fileName.py in the command line.

Any ideas as to why the crontab does not execute?

Thanks

  • 2
    try this: 2 * * * * /usr/bin/python3 /home/pi/path_to_script/fileName.py – Steve Robillard Apr 14 '16 at 19:29
  • Unfortunately that did not work – RPiNoob_16 Apr 14 '16 at 19:37
  • I am assuming that you are replacing the path_to_script and filename.py with those for your script. Also who owns the file and what are the permissions on it? – Steve Robillard Apr 14 '16 at 19:39
  • 2
    Is there the needed blank line after the crontab entry? – joan Apr 14 '16 at 20:52
  • 1
    print() lines in the script and those messages are never outputted that is not going to work. There's typically nowhere to output to when running from cron. – Ghanima Apr 14 '16 at 20:57
2

cron jobs run with different environment variables than your usual terminal session, most notably a more limited PATH. Copy the following to a file (test.sh), run it with env -i sh test.sh, and see what happens,

#!/bin/sh
HOME=/home/pi
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/home/pi
/home/pi/path_to_script/fileName.py
  • Would I make this .sh executable and make the crontab call this .sh file? – RPiNoob_16 Apr 15 '16 at 18:28
  • Assuming its called test.sh, run it with env -i sh test.sh, you don't even have to chmod test.sh, and the env will clear the environment first. – jamieguinan Apr 15 '16 at 18:33
  • Run the env ... right from the shell, not crontab. – jamieguinan Apr 15 '16 at 18:35
0

Given that this is the Raspberry Pi forum, there's a big chance you're accessing GPIOs in your Python script - correct me if I'm wrong and I'll shamefully hit the delete button :P

Anyway, if I'm right try adding your script to root's crontab via sudo crontab -e and keep the absolute path like you're doing here: */2 * * * * /usr/bin/python3 /home/pi/path_to_script/fileName.py

As @joan mentioned in the comments, you need to have an empty line / newline after a cron entry so make sure you hit Enter once after entering/pasting the line into the crontab.

  • Unfortunately I am not accessing any GPIO pins. Is sudo crontab -e necessary when only accessing GPIO pins? The RPi is accessing another device via Modbus in the fileName.py. I simply want the crontab to start fileName.py – RPiNoob_16 Apr 15 '16 at 18:14
  • @RPiNoob_16 Ok. sudo/root privileges are required for various things; not just GPIO access - it often causes trouble because Linux is quite restrictive out of the box, the Pi user is unprivileged per default and everybody wants to play around with GPIO. Anything requiring sudo should be able to run without it if it's placed in root's crontab which you access via sudo crontab -e. The @reboot directive often only works in root's crontab as well. Anyway, if you've tried these things to no avail, I'll re-consider and eventually edit or delete my answer. – jDo Apr 15 '16 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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