強制PHP命令列指令碼單進程啟動並執行方法_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
/**
* 保證單進程
*
* @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");
}

http://www.bkjia.com/PHPjc/754797.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/754797.htmlTechArticle複製代碼 代碼如下: /** * 保證單進程 * * @param string $processName 進程名 * @param string $pidFile 進程檔案路徑 * @return boolean 是否繼續執行當前進程...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.