PHP 检查进程 ID [英] PHP Check Process ID

查看:48
本文介绍了PHP 检查进程 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想知道一段时间并决定询问它的事情.

This is something i have wondered for a while and decided to ask about it.

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

We have the function getmypid() which will return the current scripts process id. Is there some kind of function such as

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

checkifpidexists() in php? I mean a inbuilt one and not some batch script solution.

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

And is there a way to change a scripts pid?

一些说明:

我想检查一个 pid 是否存在,看看脚本是否已经在运行,所以它不会再次运行,如果你愿意,可以使用人造 cron 作业.

I want to check if a pid exists to see if the script is already running so it dont run again, faux cron job if you will.

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

The reason i wanted to change the pid is so i can set the script pid to something really high such as 60000 and hard code that value so this script can only run on that pid so only 1 instance of it would run

编辑----

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

To help anyone else with this proplem, i have created this class:

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';
}

推荐答案

在 linux 中,您可以查看/proc.

In linux, you would look at /proc.

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

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

In Windows you could shell_exec() tasklist.exe, and that would find a matching process 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文件.

I believe what you want to do is maintain a PID file. In the daemons I've written, they check a config file, look for an instance of a pid file, get the pid out of the pid file, check to see if /proc/$pid exists, and if not, delete the pid file.

   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 文件.

Process IDs are granted by the OS and one cannot reserve a process id. You would write your daemon to respect the pid file.

这篇关于PHP 检查进程 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆