3

I have python script which runs fine. I want to run it as a cron job every 10 minutes

I've done crontab -e and entered the line

*/10 * * * * root python /path/to/script/script.py

It doesn't seem to be running.

So I've played around on the command line and seeing at which point it stops running. It runs in it's directory and every one preceeding it until I move out of my home directory (ie if I'm in /home/henry/ then it runs but if I'm in, for example, /var then it doesn't) when I get a permission error trying to write to a file.

# save this for next time
with open(mycsvfile, "w") as outfile:
    writer = csv.writer(outfile)
    writer.writerows([s.strip().encode("utf-8") for s in row ]for row in list_of_rows)
outfile.close()

I've already done chmod +x /home/user/Location/Of/Script to ensure the script has access (I thought). What am I missing? Thanks for your help

  • "*/10 * * * * root python /path/to/script/script.py" is for /etc/crontab not crontab -e. – Rinzwind Nov 2 '16 at 15:09
4

Remove the string root (assuming python exists in the cron defined PATH, otherwise use absolute path e.g. /usr/bin/python):

*/10 * * * * python /path/to/script/script.py
*/10 * * * * /usr/bin/python /path/to/script/script.py

Why:

  • When you use crontab -e to open a cron table, you are opening the invoking user's crontab, no username field is allowed (unlike /etc/crontab and /etc/cron.d/*)

  • As it stands now, you are running the command root (which presumably is not available) with the arguments python and /path/to/script/script.py

Also if you have made the script executable, you should add a shebang indicating the script interpreter (e.g. /usr/bin/python) instead of running the script as an argument to the interpreter. Then you can do:

*/10 * * * * /path/to/script/script.py
  • It has the shebang #!/usr/bin/python. Does that mean I can remove the usr/bin/python ? – HenryM Nov 2 '16 at 15:11
  • @HenryM Yes.... – heemayl Nov 2 '16 at 15:11
  • but it still isn't running? – HenryM Nov 2 '16 at 15:19
  • @HenryM Make sure the script is executable. If it does not run still, 1)Check /var/log/syslog for cron entries 2)set the job as */10 * * * * /usr/bin/python /path/to/script/script.py >/tmp/cron_log 2>&1, and then check the content of /tmp/cron_log after run. – heemayl Nov 2 '16 at 15:22
  • my syslog has the following two lines everytime it runs. (henry) CMD (/home/henry/Documents/Scraper/clint.py) (CRON) info (No MTA installed, discarding output). The first is the script and the 2nd is because I do not have a mail server installed. But the output file the job is writing to isn't being updated. – HenryM Nov 2 '16 at 15:52

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.