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.

user3262424
  • 7,223
  • 16
  • 54
  • 84

1 Answers1

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.

Aphex
  • 7,390
  • 5
  • 33
  • 54
  • 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? – user3262424 Dec 20 '10 at 17:55
  • 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` – Aphex Dec 20 '10 at 18:07
  • 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? – user3262424 Dec 20 '10 at 18:42
  • Check the `MAILTO` field in `/etc/crontab`. By default it mails stuff to `root`. – Aphex Dec 20 '10 at 18:44
  • 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"]) – user3262424 Dec 20 '10 at 18:51
  • You have to pipe the output of the main 'scheduler' script executed by cron. Add a `> logfile.log` to its line in `crontab`. – Aphex Dec 20 '10 at 19:17
  • Regarding redirecting the output (i.e. ```print``` and ```puts``` statements) of a script to a log file (or something similar) that is executed as a cron job in ```/etc/crontab```, there is already discussion (with an answer). Checkout this [link](http://stackoverflow.com/questions/1887618/how-to-redirect-complete-output-of-a-script) – 9monkeys May 15 '12 at 20:13