Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Cron start a new job if the current job is not complete? [duplicate]

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 ?

like image 542
Seif Hatem Avatar asked Jan 19 '13 00:01

Seif Hatem


2 Answers

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:

  • Shell script
  • Python
  • PHP
  • Perl
like image 127
ernie Avatar answered Oct 15 '22 22:10

ernie


They will both run at the same time.

like image 24
Rob Avatar answered Oct 15 '22 23:10

Rob