Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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 ?
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
Code:
while ps -C night_job_name > /dev/null
do
sleep 300
done
where night_job_name is the name of the script launched by the night cronjob.
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.
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.