Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I get cron to run a python script as root? Here is my crontab file:

0 * * * * ./twitter/twitter.py

Am I doing something wrong?

share|improve this question

1 Answer 1

If that's root's crontab (edited with sudo crontab -u root -e or su -c 'crontab -u root -e' or similar), then ./twitter/twitter.py will run every hour. If this is the system crontab (/etc/crontab), a sixth field is needed after the asterisks: 0 * * * * root …. I recommend using the root user's crontab and leaving the system crontab to the system.

./twitter/twitter.py starts from the current directory. Cron can't guess what you want the current directory to be: you never told it. Change this to use the absolute path to the script, e.g. /home/paul/scripts/twitter/twitter.py.

You'll need to make sure that twitter.py starts with #!/usr/bin/env python (I'm assuming it is a Python script) and that python is in cron's default PATH (this will depend on your brand of unix; you can be sure that /usr/bin is in the default PATH, but if your python lives elsewhere such as /usr/local/bin, you may need to add a line like PATH=/usr/local/bin:/bin:/usr/bin at the top of the crontab).

Also make sure that the script is executable (chmod +x …/twitter.py).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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