How to have cron run a python script as root?

8,107

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:
8,107

Related videos on Youtube

AndiDog
Author by

AndiDog

Updated on September 17, 2022

Comments

  • AndiDog
    AndiDog over 1 year

    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?