如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9874331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:50:28  来源:igfitidea点击:

How to check whether specified PID is currently running without invoking ps from PHP?

phpprocesspid

提问by anonymous-one

We would like to check if a specified process is currently running via PHP.

我们想检查指定的进程当前是否正在通过 PHP 运行。

We would like to simply supply a PID and see if it is currently executing or not.

我们想简单地提供一个 PID,看看它当前是否正在执行。

Does PHP have an internal function that would give us this information or do we have to parse it out of "ps" output?

PHP 是否有一个内部函数可以为我们提供这些信息,还是我们必须从“ps”输出中解析它?

回答by Nasreddine

If you are on Linux, try this :

如果你在 Linux 上,试试这个:

if (file_exists( "/proc/$pid" )){
    //process with a pid = $pid is running
}

回答by Wandering Zombie

posix_getpgid($pid);will return false when a process is not running

posix_getpgid($pid);当进程未运行时将返回 false

回答by Steel Brain

If you want to have a function for it then:

如果你想拥有它的功能,那么:

$running = posix_kill($pid,0);

Send the signal sig to the process with the process identifier pid.

将信号 sig 发送到进程标识符为 pid 的进程。

Calling posix_killwith the 0 kill signal will return trueif the process is running, falseotherwise.

如果进程正在运行,则posix_kill使用 0 kill 信号调用将返回truefalse否则将返回。

回答by Pierre-Olivier

I would call a bash script using shell_exec

我会调用一个 bash 脚本使用 shell_exec

$pid = 23818;
if (shell_exec("ps aux | grep " . $pid . " | wc -l") > 0)
{
    // do something
}

回答by consatan

I think posix_kill(posix_getpgrp(), 0)is the best way to check if PID is running, it's only not available on Windows platforms.

我认为这posix_kill(posix_getpgrp(), 0)是检查 PID 是否正在运行的最佳方法,它仅在 Windows 平台上不可用。

It's the same to kill -0 PIDon shell, and shell_exec('kill -0 PID')on PHP but NO ERROR output when pid is not exists.

它与kill -0 PIDshell 和shell_exec('kill -0 PID')PHP相同,但在 pid 不存在时没有 ERROR 输出。

In forked child process, the posix_getpgidreturn parent's pid always even if parent was terminated.

在分叉的子进程中,posix_getpgid即使父进程终止,也始终返回父进程的 pid。

<?php

$pid = pcntl_fork();

if ($pid === -1) {
    exit(-1);
} elseif ($pid === 0) {
    echo "in child\n";
    while (true) {
        $pid = posix_getpid();
        $pgid = posix_getpgid($pid);
        echo "pid: $pid\tpgid: $pgid\n";
        sleep(5);
    }
} else {
    $pid = posix_getpid();
    echo "parent process pid: $pid\n";
    exit("parent process exit.\n");
}

回答by Sven Lauber

i have done a script for this, which im using in wordpress to show game-server status, but this will work with all running process on the server

我为此做了一个脚本,我在 wordpress 中使用它来显示游戏服务器状态,但这将适用于服务器上的所有正在运行的进程

<?php
//##########################################
// desc: Diese PHP Script zeig euch ob ein Prozess l?uft oder nicht
// autor: seevenup
// version: 0.2
//##########################################

if (!function_exists('server_status')) {
        function server_status($string,$name) {
                $pid=exec("pidof $name");
                exec("ps -p $pid", $output);

                if (count($output) > 1) {
                        echo "$string: <font color='green'><b>RUNNING</b></font><br>";
                }
                else {
                        echo "$string: <font color='red'><b>DOWN</b></font><br>";
                }
        }
}

//Beispiel "Text zum anzeigen", "Prozess Name auf dem Server"
server_status("Running With Rifles","rwr_server");
server_status("Starbound","starbound_server");
server_status("Minecraft","minecarf");
?>

more information here http://umbru.ch/?p=328

更多信息在这里http://umbru.ch/?p=328

回答by Vitaliy Kornienko

//For Linux
$pid='475678';
exec('ps -C php -o pid', $a);
if(in_array($pid, $a)){
    // do something...
}

回答by Denis Matafonov

Here is how we do it:

这是我们如何做到的:

if (`ps -p {$pid} -o comm,args=ARGS | grep php`) {

  //process with pid=$pid is running;
}

回答by Leonid Zakharov

$pid = 12345;
if (shell_exec("ps ax | grep " . $pid . " | grep -v grep | wc -l") > 0)
{
    // do something
}