Ubuntu – How to set permissions so job runs from cron

cronpermissionsscripts

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

Best Answer

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
Related Question