How to make sure a script only runs after another script
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.
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?
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.