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

強(qiáng)制PHP命令行腳本單進(jìn)程運(yùn)行的方法

復(fù)制代碼 代碼如下:
 /**
  * 保證單進(jìn)程
  *
  * @param string $processName 進(jìn)程名
  * @param string $pidFile 進(jìn)程文件路徑
  * @return boolean 是否繼續(xù)執(zhí)行當(dāng)前進(jìn)程
  */
 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;
  }
 }

 /**
  * 獲取當(dāng)前進(jìn)程對應(yīng)的Command
  *
  * @return string 命令及其參數(shù)
  */
 function getCurrentCommand()
 {
  $pid     = posix_getpid();
  $command = exec("/bin/ps -p $pid -o command=");

  return $command;
 }

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

php技術(shù)強(qiáng)制PHP命令行腳本單進(jìn)程運(yùn)行的方法,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 光泽县| 平湖市| 淮滨县| 岳池县| 梧州市| 佛学| 津市市| 竹北市| 舞钢市| 沙湾县| 乌审旗| 桓仁| 马公市| 班戈县| 仁怀市| 白城市| 汕头市| 峡江县| 留坝县| 汉寿县| 光泽县| 仁布县| 利津县| 卓尼县| 临海市| 边坝县| 天峻县| 永登县| 仲巴县| 富源县| 井冈山市| 庆城县| 通榆县| 恩施市| 栖霞市| 江孜县| 宜城市| 鹿邑县| 桐梓县| 同江市| 秭归县|