How to check php job instance

On my system, I planned to execute a PHP job every minute.

Now I want that if 1 instance of this task is launched and takes more than 1 minute, a new instance that starts after each minute checks if any previous instance is working, and if there is any previous instance, the new one kills itself. .. I tried to use the system () commands, but I cannot figure it out.

Any sugestions

+3
source share
2 answers

Use exclusive file lock in the lock file. As soon as the process completes, 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

+8
source

. , , - , . .

:

if ( flag is set )
  exit

set flag

do some stuff

clear flag

, . memcache . - .

+1

Source: https://habr.com/ru/post/1787939/


All Articles