Cron and Python virtualenv

I created a simple twitter bot that consumes data from a REST API and tweets on certain conditions. The data from the REST API changes infrequently so the bot is set to run every half hour. The bot is written in python and uses a virtualenv so the crontab entry needs to use that virtualenv as well. This post describes what I did to achieve that.

To create a crontab entry:

  1. Login as the user that will run the process.
  2. Use the command crontab -e to create/edit cron entries.

The command I used was:

0,30 * * * * cd /home/myuser/myproject && venv/bin/python bot.py config/config.ini > /dev/null 2>&1

This command does the following:

0,30 * * * *

  • Run at 0 and 30 minutes of every hour of every day
  • Aside: An alternative cron setting would be to run every 30 minutes which would be: */30 * * * *. However, this means it would run relative to when the crontab was created or when the machine was rebooted. I wanted it to run at specific times so provided concrete times.

cd /home/myuser/myproject

  • Change directory to where my bot is located

&&

  • If the cd command completed successfully then run the following command

venv/bin/python bot.py config/config.ini

  • venv is the location of my virtualenv relative to /home/myuser/myproject
    • So, the bot.py module is run with the virtualenv’s version of python.
  • config/config.ini is passed in as an argument to bot.py.

> /dev/null 2>&1

  • Redirects both stderr and stdout to /dev/null
  • For testing purposes you may want to see the output of cron so either:
    1. Remove the > /dev/null 2>&1
      • This will mail the user the result of the cron job
      • You will need to have the mail command installed on your system (mailx package on Centos/Redhat and mailutils package on Ubuntu/Debian)
    2. Redirect the output to a log file (ex: > /tmp/cron.log 2>&1)

To view existing crontab use:

crontab -l

References:

http://stackoverflow.com/questions/3287038/cron-and-virtualenv

http://alvinalexander.com/linux/unix-linux-crontab-every-minute-hour-day-syntax