My approach should always keep crontab as simple as possible and consider all the configurations inside scripts called crontab.
1) Create a shell script: e.g. /var/webapp/cron.sh
#!/bin/sh PATH="/var/webapp/.env/bin:$PATH" export PATH cd /var/webapp/ python test.py
where /var/webapp/.env/bin is the virtual location. When installing PATH you do not need to start the source ../ activate
2) Set the environment correctly. For example, for a Django application:
#!/usr/bin/env python import os from datetime import datetime os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.production' os.environ['DJANGO_CONF'] = 'settings.production' from util.models import Schedule dset = Schedule.objects.all() for rec in dset: print rec print 'cron executed %s' % datetime.today()
In this example, the django settings are in the /production.py settings
3) Finally, edit / etc / crontab. For example, every half hour, every day:
1,31 * * * * root /var/webapp/cron.sh >> /var/webapp/cron.log
Please note that it is important to generate logs to help you find errors or debug messages.
Josir
source share