How to prevent PHP script running more than once?

前端 未结 6 908
臣服心动
臣服心动 2020-12-05 16:23

Currently, I tried to prevent an onlytask.php script from running more than once:

$fp = fopen(\"/tmp/\".\"onlyme.lock\", \"a+\");
if (flock($fp,         


        
6条回答
  •  温柔的废话
    2020-12-05 16:57

    try using the presence of the file and not its flock flag :

    $lockFile = "/tmp/"."onlyme.lock";
    if (!file_exists($lockFile)) {
    
      touch($lockFile); 
    
      echo "task started\n";
      //
      // do something lengthy
      //
    
      unlink($lockFile); 
    
    } else {
      echo "task already running\n";
    }
    

提交回复
热议问题