tags:

views:

127

answers:

4

I need to stop the overlapping of cron jobs for example:If a cron job is scheduled at morning 2 o clock for DB backup and other cron job is scheduled at morning 7 o clock for DB backup again.So i need to stop the 7 o clock scheduled cron job if the DB backup for 2 o clock is not completed.

A: 

You can do that just using cron. You are going to have to add some logic to your script or code to check if the other one is running or not. A total hack would be to have your script check for the exists of a particular file. If it does not exist then you should create this file, perform the backup, then remove it. If the file does exist, do nothing.

That is a hack and only really works because your jobs are spaced so far apart.

Chris J
+3  A: 

The most straightforward way to do this would be to have your 2am and 7am tasks coordinate. So for instance, the DB backup script could create a lock file when it executes, and check for the existence of this file when it starts up. If the file exists it could simply exit without doing any work (or sleep for a bit, or whatever you decide).

Doing this in a completely task-agnostic way might be possible, but it's going to be trickier and unnecessary in this particular case.

Andrzej Doyle
A lock file would have to be managed (if backup script crashes and lock file not cleaned up), or perhaps ignored if older than 24 hrs. Better to ask the database if it is being backed up (if that is possible in this case).
jowierun
+1  A: 

You just need something that you can check to see if the first task has finished running, and this check should be done right before the job runs again.

I'd implement this with a database (if available) or a registery key or even a text file. So your task would look like this:

  read job_flag
  if job_flag == 0
    step job_flag = 1
    run job
    step job_flag = 0
  else
    stop job 
  end
Ciaran Archer
+2  A: 

Assuming you are on a Unix system: The easiest way to do this is create a file and write the cron job's PID to it. If the file can't be created using open(2) with O_CREAT|O_EXCL flags, it already exists, thus the previous job is still running or has crashed without cleaning up. The PID can be used to determine whether the job is still running.

In your case, the safest approach is probably for the cron job to die if the PID file is found on startup.

Since you put the perl tag to the question, I suggest to have a look at the File::Pid and Proc::PID::File modules.

hillu