4

I have a 'long' python script that takes roughly 45[min] to run. I use another (a 'scheduler' script) python script to run this long script. When I run the 'scheduler' script using the terminal, everything works perfectly (meaning, the 'long' script runs without any issues).

I had some struggles, but eventually succeeded adding the 'scheduler' script to run through cron every minute. so it now 'runs' other script and works OK.

Here is the problem: whenever a script (that is being 'run' by the 'scheduler') has a line that says:

print "hello"

or any 'print' statement, the cron job runs, but terminates after 20-30 seconds. When I remove any 'print' statements, cron runs the jobs normally and does not terminate.

I'd like to fix this situation, and have the scripts continue to run even if they have some 'print' statements in them. any hints how to do it?

P.S. from within the 'scheduler', I use

subprocess.Popen([sys.executable, command])

to 'run' all other python scripts.

1 Answer 1

6

I have a hunch that it's due to the way cron handles stdout. How are you redirecting output?

From http://aplawrence.com/BDD/bbcronbasics.html:

OUTPUT

Normally the output of any program is sent to STDOUT (standard out) and usually winds up on someone's display screen. For jobs started by cron, STDOUT will be directed to the local mail subsystem, which means any output generated by a cron job will be mailed to the user owning the job. If this is not desirable you should take steps to redirect your cron job's output to a file. It is also suggested you redirect STDERR (standard error) to a file so as to be able to analyze any anomalies in your cron job's execution.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. I am not sure how to direct the output at present. Any ideas? also, I never got an mail from cron. are there any settings I need to define?
By mail they mean the local mail system that every *nix user has on their own system. To redirect output to a file, you can use the > redirection operator: python scheduler.py > output.log
Thank you Aphex. So now I know how to direct the output to a file. I did set up an email account in Evolution (I used Ubuntu 10.04) and never received an email from Cron. How can I fix it?
Check the MAILTO field in /etc/crontab. By default it mails stuff to root.
Thanks. By the way, my cron doesn't work when I direct the script to an output (it does work from the terminal). Here's how I direct it: subprocess.Popen([sys.executable, "script.py > log.log"])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.