色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

強制PHP命令行腳本單進程運行的方法

復制代碼 代碼如下:
 /**
  * 保證單進程
  *
  * @param string $processName 進程名
  * @param string $pidFile 進程文件路徑
  * @return boolean 是否繼續執行當前進程
  */
 function singleProcess($processName, $pidFile)
 {
  if (file_exists($pidFile) && $fp = @fopen($pidFile,"rb"))
  {
   flock($fp, LOCK_SH);
   $last_pid = fread($fp, filesize($pidFile));
   fclose($fp);

   if (!empty($last_pid))
   {
    $command = exec("/bin/ps -p $last_pid -o command=");

    if ($command == $processName)
    {
     return false;
    }
   }
  }

  $cur_pid = posix_getpid();

  if ($fp = @fopen($pidFile, "wb"))
  {
   fputs($fp, $cur_pid);
   ftruncate($fp, strlen($cur_pid));
   fclose($fp);

   return true;
  }
  else
  {
   return false;
  }
 }

 /**
  * 獲取當前進程對應的Command
  *
  * @return string 命令及其參數
  */
 function getCurrentCommand()
 {
  $pid     = posix_getpid();
  $command = exec("/bin/ps -p $pid -o command=");

  return $command;
 }

使用方法:
復制代碼 代碼如下:
if (singleProcess(getCurrentCommand(), 'path/to/script.pid'))
{
    // code goes here
}
else
{
 exit("Sorry, this script file has already been running .../n");
}

php技術強制PHP命令行腳本單進程運行的方法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 乡城县| 阿坝县| 黄陵县| 黔南| 新竹县| 孟村| 花垣县| 商南县| 青铜峡市| 林甸县| 漠河县| 绥德县| 阳高县| 松潘县| 汾西县| 彰化县| 华安县| 攀枝花市| 盘山县| 田东县| 军事| 如东县| 长葛市| 孝感市| 桐庐县| 武陟县| 稻城县| 万盛区| 怀宁县| 阜城县| 竹北市| 会东县| 建阳市| 蓬安县| 红原县| 普洱| 垣曲县| 江川县| 嘉荫县| 乳山市| 辰溪县|