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
(assumingpython
exists in thecron
definedPATH
, otherwise use absolute path e.g./usr/bin/python
):Why:
When you use
crontab -e
to open a cron table, you are opening the invoking user'scrontab
, 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 argumentspython
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: