欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年04月15日 09:56:39   作者:  
本文介紹了一個(gè)強(qiáng)制PHP在單進(jìn)程中執(zhí)行的函數(shù),多用在php命令行中和一些特殊需求的地方,需要的朋友可以參考下

復(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)程對(duì)應(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");
}

相關(guān)文章

最新評(píng)論