Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have setup a list of cron, Some of the cron script takes a long time to run (like 1-5 hours, and they are increasing every day). I do not want to run two cron scripts at the same time, as I do not have the resources or need for it. I need to find a solution so that the scheduled scripts will not start until the other previous script has finished. I have 10-15 cron job in the list, where I don't want to overlap 5 of them.

Does anyone have any suggestions on what kind of solution I should be looking more closely at to achieve my goal?

share|improve this question

5 Answers 5

A. Create temporary lockfile which will be used by the script while it's running, if new script will find this file - it will stop.

i.e.

if [ -a lockfile ]; then
exit 0
else
touch lockfile
fi
... rest of code...
rm -f lockfile

B. Check with the script if another one is already running like ps auxwf|grep blabla.

share|improve this answer
    
Hi thanks, but do you know any API that handle the things? like i see some of them like resque-scheduler (ruby on rail) / Celery (python). is there anything like this in PHP? –  Shamsul Dec 10 '12 at 12:36
    
@Shamsul No reason to overcomplicate things. Lock files work just fine for your use case and take about a minute to build in. –  ceejayoz Dec 10 '12 at 14:56
    
Agree with ceejayoz. No need to have API and complicate things, ex: file locking is used to for this: php.net/manual/en/function.flock.php –  GioMac Dec 11 '12 at 21:46

You can work with lockfiles in PHP too. I solved exactly the same problem with code:

$lockfile='/tmp/'.__CLASS__.'.lock'; // the lockfile should be different for each script
$lock=fopen($lockfile, 'w');
if(!flock($lock, LOCK_EX|LOCK_NB)){
//  $this->log("This process is already started, i'm exiting.");
    exit;
}

Place this at the beginning of you job script.

share|improve this answer

Using lock file approach may lead to situation wher daily cronjob wont start. Have you considered running those jobs from one cronjob sequentally

#!/bin/bash/
sh /jobs/somejob1
sh /jobs/somejob2

I recommend running i/o heavy apps sequentally from one job

share|improve this answer
    
No, let me cleat the things a little more.. The corn list are like i have scheduled 1st corn at 8:00 PM every day, and then 2nd at 11:00, 3rd at 1:00 AM, so if the 1st job stop by 11:00 that's good but the problem is its sometime takes 12:00 to finish and my 2nd job starts and i do not want it to start at 11 it will start at 12:00 but 3rd job will also depends start on finishing on the 2nd job. I think its more clear now. –  Shamsul Dec 10 '12 at 14:58

There is a package called lockfile-progs available in any debian-derived (and probably other) distribution, and it works.

share|improve this answer

The very same topic came up on Sysadvent earlier this month: http://sysadvent.blogspot.co.uk/2012/12/day-4-zookeeper-for-distributed.html

You can use Zookeeper as a distributed lock manager for lockfiles across a cluster of machines.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.