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 ?
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:
They will both run at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With