31

Possible Duplicate:
How to prevent the cron job execution, if it is already running

I have a cron job that may take 2 mins or may take 5 hours to complete. I need to make sure that this job is always executed.

My question is:

Will it start after the previous one is done or will they both run at the same time and mess up the database if i set it to execute every minute ?

Shafique Jamal
  • 1,550
  • 3
  • 21
  • 45
Seif Hatem
  • 1,433
  • 2
  • 12
  • 20

3 Answers3

60

They'll run at the same time. The standard practice around this is to create a file lock (commonly referred to as a flock), and if the lock isn't available, don't run.

The advantages to this over Zdenek's approach is that it doesn't require a database, and when the process ends, the flock is automatically released. This includes cases where the the process is killed, server rebooted, etc.

While you don't mention what your cron job is written in, flock is standard in most languages. I'd suggest googling for locks and the language you're using, as this will be the more robust solution, and not relying upon random timeouts. Here are some examples from SO:

Community
  • 1
  • 1
ernie
  • 6,356
  • 23
  • 28
16

They will both run at the same time.

Rob
  • 12,659
  • 4
  • 39
  • 56
5

I would suggest to record it in database. I would record when script started and if task was successfully finished. The script should also update record in db - something like every 5 mins - I am still running.

When new job is started, it should check if all previous tasks are finished or last update was more then then minutes (?) ago, if yes then run, no then exit.

This way you know that previous script is running or if it died, job which is not finished and hasn't been updated for 10 minutes is going to be marked as finished.

I should mention this solution might be better than flock when you have multiple servers and task can be triggered on any of them.

Zdenek Machek
  • 1,758
  • 1
  • 20
  • 30
  • 7
    Use flock(), not this. You're reinventing the wheel here, and you have the ambiguity of using a time window for if a script is still alive or not (which if you do pick this approach, should be set to be longer than the interval between cron runs.) – ernie Jan 19 '13 at 00:21
  • 3
    [File locking](http://en.wikipedia.org/wiki/File_locking) is standard on most any modern OS, and most modern languages, and is much more robust than relying upon an inactivity timeout. As the OP mentioned cron, they're likely on linux, which definitely has flock() built in. Perl, PHP, Python, etc all do as well. – ernie Jan 19 '13 at 00:31
  • 2
    And a database is a much heavier piece of "additional software" than using standard OS functionality . . . – ernie Jan 19 '13 at 00:42