Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a python script as a cron job that is called like this

/path/to/python /path/to/myscript myparam

When an error occurs in the script, the cronjob silently fails. When I run the same command from the command line, I get a stack trace. How can I bring my script to output the stack trace (or write it to a file) when running as a cron job? I have already set the MAILTO variable. I have also tried putting

/path/to/python /path/to/myscript myparam 2>&1 >> /path/to/logfile

in the crontab but logfile remains empty.

Update: The call without output redirection works now. It was not an issue with the python script, but a wrong separator in the MAILTO crontab setting. Remember: Use commas and don't put spaces after the commas!

share|improve this question
up vote 2 down vote accepted

You used the wrong order.

  • 2>&1 means that redirect standard error to the standard output and both to the terminal
  • >> /path/to/logfile redirects standard output to the log file. You didn't mentioned the standard error, so it still go to the terminal.

Try this:

/path/to/python /path/to/myscript myparam >> /path/to/logfile 2>&1

Make sure that the user who run this script has write permission to the log file.

share|improve this answer

Check out the similar sitaution worked through here Adding exception handling, logging, mail, and stderr should get you the data you want on the traceback.

share|improve this answer
I was experiencing missing error output from a syntax error in the python script. Most of the suggestions in the forum only deal with syntactically correct scripts. – chiborg Sep 13 '11 at 13:42
Understood, thus my reference to 'traceback'. So you need to get that syntax error out of the crontab event black hole (no logging from the crontab will reflect the issue) via some exception handling inside your python script. Once you get that addressed the issue is passing the details to a persistent log, mail, and/or stderr. The referenced exception handling test would be a good start. – zedman9991 Sep 13 '11 at 15:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.