Copy CodeThe code is as follows:
/**
* Guaranteed Single Process
*
* @param string $processName process name
* @param string $pidFile process file path
* @return Boolean to continue executing the current process
*/
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;
}
}
/**
* Gets the command for the current process
*
* @return string command and its arguments
*/
function Getcurrentcommand ()
{
$pid = Posix_getpid ();
$command = EXEC ("/bin/ps-p $pid-o command=");
return $command;
}
How to use:
Copy the Code code as follows:
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.html www.bkjia.com true http://www.bkjia.com/PHPjc/754797.html techarticle Copy the code code as follows:/** * Guaranteed Single Process * * @param string $processName process name * @param string $pidFile process file path * @return Boolean to continue executing the current process ...