-   Linux - General (
-   -   Stop cronjobs overlapping ? (

MikeAtVillage 05-22-2008 05:13 AM

Stop cronjobs overlapping ?
Hi folks,

Is there any way I can stop my cronjobs overlapping ?

I have shell scripts called from crontab which rsnap/rsync all the data from my remote servers to a main backup server. One remote server (of 7) is backed up each night plus another backed up every morning.

I've come in this morning to find that last night's has not yet completed and this morning's has already started.... so the network is too busy for some staff to work :-(

Is there a way I can tell crontab to not start a job until a running job has completed ?



colucix 05-22-2008 05:59 AM

At the very beginning of the morning job you can check if the night job is still running and wait for its completion before proceeding. A way to do this can be

while ps -C night_job_name > /dev/null
    sleep 300

where night_job_name is the name of the script launched by the night cronjob.

MikeAtVillage 05-22-2008 06:16 AM

Excellent, I think I can manage that OK. Many thanks.

ilikejam 05-22-2008 07:15 AM

You could create a lock file in /tmp at the start of the first cron job, and remove it at the end, and have the second cron job check to see if this lock file exists.


chrism01 05-22-2008 08:33 PM

If they are dependent jobs, use the lock file, otherwise use the ps solution.

Reasons: if dependent, you don't want the 2nd job to run even if the 1st one crashes (lock file is not removed).
If you only need them to not overlap in time (as seems to be the case here),the 2nd job should still run, even if the first one crashes, ie so long as the 1st one is not actively running...

MikeAtVillage 05-23-2008 12:48 AM

Ah, thanks for that tip Chris. At the moment the scripts are not dependent so I'll use the ps method. At some time in the near future I am planning to modify them - run the rsync script and when completed tar entire backup directory and move to an archive. When I start using that I will probably need to employ lock file method.



chrism01 05-23-2008 01:08 AM

The other option for that is for script1 to call script2 as its last cmd ...

All times are GMT -5. The time now is 01:07 AM.