【问题标题】:PHP Check Process IDPHP 检查进程 ID
【发布时间】:2010-12-11 23:56:33
【问题描述】:

这是我一直想知道并决定询问它的事情。

我们有函数 getmypid() 将返回当前脚本进程 ID。是否有某种功能,例如

checkifpidexists() 在 php 中?我的意思是一个内置的,而不是一些批处理脚本解决方案。

有没有办法改变脚本的 pid?

一些澄清:

我想检查一个 pid 是否存在,看看脚本是否已经在运行,所以它不会再次运行,如果你愿意的话,是假的 cron 作业。

我想更改 pid 的原因是我可以将脚本 pid 设置为非常高的值,例如 60000 并硬编码该值,因此该脚本只能在该 pid 上运行,因此只有 1 个实例会运行

编辑----

为了帮助其他人解决这个问题,我创建了这个类:

class instance {

    private $lock_file = '';
    private $is_running = false;

    public function __construct($id = __FILE__) {

        $id = md5($id);

        $this->lock_file = sys_get_temp_dir() . $id;

        if (file_exists($this->lock_file)) {
            $this->is_running = true;
        } else {
            $file = fopen($this->lock_file, 'w');
            fclose($file);
        }
    }

    public function __destruct() {
        if (file_exists($this->lock_file) && !$this->is_running) {
            unlink($this->lock_file);
        }
    }

    public function is_running() {
        return $this->is_running;
    }

}

然后你像这样使用它:

$instance = new instance('abcd'); // the argument is optional as it defaults to __FILE__

if ($instance->is_running()) {
    echo 'file already running';    
} else {
    echo 'file not running';
}

【问题讨论】:

  • “所以我可以将脚本 pid 设置为非常高的值,例如 60000 并硬编码该值”——假设您可以设置 PID 是有缺陷的。这种方法行不通。

标签: php process


【解决方案1】:

在 linux 中,您会查看 /proc。

return file_exists( "/proc/$pid" );

在 Windows 中,您可以使用 shell_exec() tasklist.exe,它会找到匹配的进程 ID:

$processes = explode( "\n", shell_exec( "tasklist.exe" ));
foreach( $processes as $process )
{
     if( strpos( "Image Name", $process ) === 0
       || strpos( "===", $process ) === 0 )
          continue;
     $matches = false;
     preg_match( "/(.*?)\s+(\d+).*$/", $process, $matches );
     $pid = $matches[ 2 ];
}

我相信您想要做的是维护一个 PID 文件。在我编写的守护进程中,他们检查配置文件,查找 pid 文件的实例,从 pid 文件中获取 pid,检查 /proc/$pid 是否存在,如果不存在,则删除 pid文件。

   if( file_exists("/tmp/daemon.pid"))
   {
       $pid = file_get_contents( "/tmp/daemon.pid" );
       if( file_exists( "/proc/$pid" ))
       {
           error_log( "found a running instance, exiting.");
           exit(1);
       }
       else
       {
           error_log( "previous process exited without cleaning pidfile, removing" );
           unlink( "/tmp/daemon.pid" );
       }
   }
   $h = fopen("/tmp/daemon.pid", 'w');
   if( $h ) fwrite( $h, getmypid() );
   fclose( $h );

进程 ID 由操作系统授予,不能保留进程 ID。你会编写你的守护进程来尊重 pid 文件。

【讨论】:

  • pcntl_fork() 不会改变当前进程的 pid! pcntl_fork() 函数创建一个子进程,该子进程与父进程的区别仅在于它的 PID 和 PPID。请参阅您系统的 fork(2) 手册页以了解有关 fork 如何在您的系统上工作的具体细节。
  • 嗯,我希望有一种更跨操作系统的方式来检查 pid 是否存在:(
  • 通过我的编辑,我提到我不认为他想要 pcntl_fork(),他也不会选择 pid。
  • Windows 的循环应该是这样的:<? foreach ($processes as $process) { if (preg_match('/^(.*)\s+'.$pid.'/', $process)) { return true; } } ?>
【解决方案2】:

实现此目的的更好方法是使用 pid 或锁定文件。只需检查 pid 文件是否存在,根据需要创建它,然后使用正在运行的 pid 填充它。

<?
  class pidfile {
    private $_file;
    private $_running;

    public function __construct($dir, $name) {
      $this->_file = "$dir/$name.pid";

      if (file_exists($this->_file)) {
        $pid = trim(file_get_contents($this->_file));
        if (posix_kill($pid, 0)) {
          $this->_running = true;
        }
      }

      if (! $this->_running) {
        $pid = getmypid();
        file_put_contents($this->_file, $pid);
      }
    }

    public function __destruct() {
      if ((! $this->_running) && file_exists($this->_file)) {
        unlink($this->_file);
      }
    }

    public function is_already_running() {
      return $this->_running;
    }
  }
?>

并按如下方式使用:

<?
  $pidfile = new pidfile('/tmp', 'myscript');
  if($pidfile->is_already_running()) {
    echo "Already running.\n";
    exit;
  } else {
    echo "Started...\n";
  }
?>

这里没有太多错误检查,但快速运行表明这在我的系统上有效。

【讨论】:

  • 问题是在做Apache reset的时候没有触发__destruct
【解决方案3】:

用于检查我使用的 Windows 机器上是否存在 PID:

function pidExists($pid)
{
    exec('TASKLIST /NH /FO "CSV" /FI "PID eq '.$pid.'"', $outputA );
    $outputB = explode( '","', $outputA[0] );
    return isset($outputB[1])?true:false;
}

请注意,如果 pid 确实不存在,$outputB[0] 包含无法找到 pid 的消息!所以为了验证我使用第二个参数。

编辑:

为了对此进行扩展,还可以使用 powershell 在后台的窗口中动态生成脚本,如下所示:

    // this function builds an argument list to parse into the newly spawned script. 
    // which can be accessed through the superglobal  global $argv;
    function buildArgList( array $arguments){
     return ' '. implode(' ', $arguments) .' ';
    }

    $arguments = buildArgList(['argument1','argument2','argument3']);
    $windowstyle = 'normal'; // or you can use hidden to hide the CLI

    pclose(popen("powershell start-process -FilePath '" . PHP_BINARY . "' -ArgumentList '-f\"" . $file . " " . $arguments . "\"' -WindowStyle " . $windowstyle,"r"));   

然后您生成的脚本可以使用:cli_set_process_title 将该进程的标题设置为某个唯一的哈希值。

在产生子进程的父进程中,您可以使用以下代码在任务列表中找到该进程,使用其窗口标题搜索唯一哈希。

exec('TASKLIST /NH /FO "CSV" /FI "windowtitle eq ' . escapeshellarg($uniquehash) . '"', $output );

当与数据库结合使用时,您基本上可以构建一个 workermanager 在不同的 php 脚本之间进行通信。

【讨论】:

  • 还有一点需要考虑的是,powershell 不能很好地处理带有空格的路径,所以这可能会阻止它工作。
【解决方案4】:

不,您不能更改任何进程的 pid。它由内核分配,是内核数据结构的一部分

【讨论】:

    【解决方案5】:

    正如其他人所说,您无法更改进程 ID - 它由操作系统内核分配并完全管理。此外,您还没有说这是基于命令行还是基于 Web 服务器:如果是后者,您甚至可能无法获取脚本的 pid。

    manual page for getmypid() 包含一些“乐观”锁定的示例。我使用 optimisitc 这个词,因为 PHP 永远不会接近像 asp.net Web 应用程序这样的应用程序,在这种应用程序中,您拥有一个真正的线程环境,其中包含共享/静态类,因此单例可以使用/滥用。基本上你可以选择:

    • 触摸文件系统上某处的“锁定文件”。然后,您的脚本会检查该文件是否存在:如果存在,则终止,否则,触摸该文件并继续处理
    • 设置基于数据库的标志来表示脚本正在运行。同上,但使用 db 表/字段将脚本标记为正在运行。

    这两者都依赖于正确终止的脚本(因为最后一步是删除锁定文件/数据库标志)。如果脚本因任何原因(或机器本身)崩溃,您可以通过手动整理过程来删除标志。对此没有简单的解决方案,但可以探索的一种途径是查看为锁添加日期标记,并采用一种任意的“如果早于 X,则最后一次运行必须崩溃”的方法。

    【讨论】:

      【解决方案6】:

      不要忘记,您可以通过反引号 (`) 访问 shell 命令,这样您就可以访问标准的 *nix 工具来处理 pid。

      来源:http://www.php.net/manual/en/language.operators.execution.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多