Cron job fails silently

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?


Solution 1:

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.

Solution 2:

First, just check that crond is running.

When cronjobs that work on the commandline fail to run as expected, it's usually an environment problem for me -- remember that the cronjob will not be running as an interactive shell.

From your commandline, run env(1) and copy these down somewhere. Next, modify your cronjob to run env so you can compare the values. Cron should email you the output (you said it was before); HD mentioned how to configure this.

5,20,35,50 * * * * env; /var/www/django-apps/callreport/util.py

Solution 3:

These are some ideas for troubleshooting your problem:

  • Check your system logs to see if the cron daemon is actually launching the script
  • If your jobs are not being logged, try increasing the logging level so that you that cron logs the start of each job (see man cron). For example, if you are running the Vixie cron daemon, this can be done by specifying the -L 1 option (or -L 2 if you also want to check when your job finishes).
  • From your Python script, as soon as it starts, create an empty file on the /tmp directory (this is another way to help you verify that the script is being called by cron and it's starting without problems)

Solution 4:

whereis python

output should be...

/usr/bin/python

then the full path to python before the script.

5,20,35,50 * * * * /usr/bin/python /var/www/django-apps/callreport/util.py