I've got a python script that I want to run via crontab. My crontab looks like this:
5,20,35,50 * * * * /var/www/django-apps/callreport/util.py
That script is set up to parse a bunch of flat files and stick the info into a MySQL db, then delete the files. It runs fine from the command line, data is copied to the db, and the flat files are removed. But nothing happens when set to run as a cron job.
In the past, I'd get a mail message when a cron job failed, but I'm not getting any feedback with this one, and I'm still feeling my way through being a sysadmin on this box. What am I doing wrong?
Answer
The usual problem with 'cron' jobs is that they have zero environment - unlike 'at' jobs which do copy your environment. When something works from the command line and not from 'cron', my experience is that 'environment' is one of the most common problems. Occasionally you run into another problem - 'cron' jobs are not run with a terminal, and occasionally programs get stroppy about this. However, this is in the 1% range compared with 99% for environment issues.
The other key technique I use is to always run a shell script from 'cron'; the shell script ensures that the environment is set correctly and then runs the real program. If a 'cron' job is giving me problems, I can then tweak the script to do useful things like this:
{
date
env | sort
set -x
...what was there before adding the debug...
} >/tmp/cron.jobname.$$ 2>&1
This redirects all the output - standard output and standard error - to a file name. You can build timestamps into the filename if you prefer that to the process ID. Analyzing the log files often reveals the problems swiftly.