某些命令行程序运行的时候,为了避免多进程互相干扰,需要让它同一时间只能有一个实例在运行。
下面是我的方法:
参考: http://blog.163.com/lgh_2002/blog/static/4401752620106161954791/
<?php
$lockfile = __FILE__.'.running';
function lock()
{
global $lockfile;
if(file_exists($lockfile))exit;
touch($lockfile);
}
function unlock()
{
global $lockfile;
unlink($lockfile);
}
lock();
register_shutdown_function('unlock');
do_something();
function do_something()
{
sleep(10);
}
?>
参考: http://blog.163.com/lgh_2002/blog/static/4401752620106161954791/