国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:22:53
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


 /**
  * 保證單進程
  *
  * @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");
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 如皋市| 漠河县| 东莞市| 兴安盟| 永嘉县| 中西区| 永昌县| 民县| 静安区| 法库县| 罗山县| 北宁市| 永和县| 高安市| 丰台区| 邵东县| 余江县| 南京市| 青铜峡市| 陆河县| 甘南县| 曲松县| 密山市| 五莲县| 区。| 长汀县| 淮滨县| 天柱县| 革吉县| 岗巴县| 宜昌市| 汝州市| 会昌县| 星子县| 勐海县| 军事| 柳河县| 鹿邑县| 南丰县| 孙吴县| 鹤壁市|