強(qiáng)制PHP命令行腳本單進(jìn)程運(yùn)行的方法
/**
* 保證單進(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;
}
使用方法:
if (singleProcess(getCurrentCommand(), 'path/to/script.pid'))
{
// code goes here
}
else
{
exit("Sorry, this script file has already been running ...\n");
}
相關(guān)文章
PHP基于進(jìn)程控制函數(shù)實(shí)現(xiàn)多線程
這篇文章主要介紹了PHP基于進(jìn)程控制函數(shù)實(shí)現(xiàn)多線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Laravel 5框架學(xué)習(xí)之Blade 簡介
本文給大家?guī)淼氖荓aravel5框架學(xué)習(xí)系列文章的第4篇,主要向大家簡單介紹下Blade,為什么要介紹它呢,因?yàn)閘aravel的模版引擎采用了blade模版引擎,so....2015-04-04

