4

I have two python scripts running as cronjobs.

ScriptA processes log files and insert records to a table, ScriptB uses the records to generate a report.

I have arranged ScriptA to run one hour before ScriptB, but sometimes ScriptB run before ScriptA finish inserting, thus generating a incorrect report.

How do I make sure ScriptB runs right after ScriptA finishes?

EDIT ScriptA and ScriptB do very different things, say, one is for saving user data, the other is for internal use. And somewhere else there maybe some ScriptC depending on ScriptA.

So I can't just merge these two jobs.

satoru
  • 31,822
  • 31
  • 91
  • 141

4 Answers4

9

Wouldn't it be better to make a single cron job that runs ScriptA and then ScriptB? That way you can be sure that ScriptB doesn't run until ScriptA finishes, and you don't need to modify either script.

The cronjob could run a simple shell script like:

#!/bin/sh

python ScriptA.py
python ScriptB.py

Edit: If you really can't merge the two cron jobs, you could use the technique described in this question to wait for the first process to finish.

Community
  • 1
  • 1
srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 3
    Beat me by a few seconds. To avoid creating wrapper script, cron can be: * * * * /usr/bin/python ScriptA.py && /usr/bin/python ScriptB.py, where && will make sure that should ScriptA finish with anything but RC0, ScriptB will not run at all. – favoretti Nov 30 '11 at 02:14
2

One approach would be to make sure that if those two jobs are separate cron jobs - there is enough time inbetween to surely cover the run of job 1.

Another approach is locking, as others here suggested, but then note, that cron will not re-run your job just because it completed unsuccessfully because of a lock. So either job2 will have to run in sleep cycles until it doesn't see the lock anymore, or on the contrary sees a flag of job 1 completions, or you'll have to get creative.

Why not to trigger 2nd script from the 1st script after it's finished and make it a single cron job?

favoretti
  • 29,299
  • 4
  • 48
  • 61
0

Make it write a file and check if the file is there.

Benjamin
  • 11,560
  • 13
  • 70
  • 119
  • "Make ScriptA write to a file, then make ScriptB runs periodically and check the file." Is this what you mean? – satoru Nov 30 '11 at 02:16
0

an approach that you could use it! is having some flag of control! somewhere, for example in the DB!

So ScriptB just runs after that flag is set! and right after it finish it it sets the flag back to default state!

Another way that you could implement that flag approach is using file system! Like @Benjamin suggested!

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73