国产探花免费观看_亚洲丰满少妇自慰呻吟_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");
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清镇市| 清涧县| 昌邑市| 苍溪县| 灵川县| 屯留县| 融水| 武宁县| 封丘县| 伊吾县| 罗城| 鹤山市| 张家港市| 库尔勒市| 铁力市| 会同县| 北宁市| 揭阳市| 应用必备| 美姑县| 行唐县| 竹溪县| 九龙城区| 兴化市| 杨浦区| 阳新县| 屯门区| 武隆县| 江安县| 衡阳市| 蒲江县| 临沭县| 永济市| 台南市| 黄大仙区| 宣威市| 康定县| 喀什市| 邢台市| 吉林市| 凤城市|