Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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 http://www.velocityreviews.com/forums/t646893-running-a-python-script-from-crontab.html. 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

 
discard

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.