Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check php job instance

In my system, I've scheduled a PHP job to run each minute.

Now I want that if 1 instance of this job is running and it takes more than 1 min, the new instance that starts after every minute checks if any previous instance is working and there is any previous instance running, the new one kills itself...I was trying the system() commands but cant figure it out.

Any sugestions

like image 536
awaiskhan200 Avatar asked Dec 05 '25 08:12

awaiskhan200


1 Answers

Use exclusive file lock on a lock file. As soon as process ends it execution, the lock is automatically released.

<?php    
$fp = fopen("/var/tmp/your.lock", "w");
if (!flock($fp, LOCK_EX|LOCK_NB)) { // try to get exclusive lock, non-blocking
    die("Another instance is running");
} 

[... continue with the script ...]

See: http://www.php.net/manual/en/function.flock.php

like image 112
vartec Avatar answered Dec 07 '25 22:12

vartec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!