hakki
hakki

Reputation: 6521

Will Cron Job Interrupt Previous Process?

I have a cron job which works every minute like below

*/1 * * * *

My script sometimes spends more than 1 minute to finish itself. My question is,

will next cron job interrupt current processing one?

For better understanding;

First script starts at 07.00 and ends 07.03 (3 minutes) Will cron intterrupt above process and start new one from start at 07.01?

Upvotes: 2

Views: 996

Answers (2)

Try using or look into flock();

What this does is, locks a file, a simple file, so, you can create a simple empty file and usa a condition to see if its locked or not, if is locked then terminate the process, if is not locked then continue with process.

try something like this:

$f = fopen('./tmp/lock.txt', 'w') or die ('path to file doesn't exist');
$lock = flock($f, LOCK_EX | LOCK_NB) or die ('lock.txt is locked, is being used');
echo "lock is free";
//continue with code here

if you are using this in a script that is being triggered by cron, be sure to use LOCK_NB, is you don´t use it, it will indicate to use the wait() command until lock.txt is unlocked.

take a look at this post

https://stackoverflow.com/a/10552054/15285033

Upvotes: 0

Eborbob
Eborbob

Reputation: 1975

Cron will start a new process each time. Once cron has started a process it then leaves it to itself and has no further control over it.

If you need your process to wait for existing ones to finish you'll need to code that into your process yourself.

Upvotes: 4

Related Questions