drbhjey445647 2012-08-03 21:46
浏览 94
已采纳

PHP exec命令使用pgrep返回值不正确

I have a php script with the following command I am running:

exec("pgrep -fl ./build-dh", $output, $return);

pgrep usually returns "1" if it does not find a "./build-dh" process running, however, it is always returning "0", even when I am positive the process is not running.

Here is what I get from $output:

Array ( [0] => 28560 sh -c pgrep -fl ./build-dh )

This means that it is outputting its' own pid, which I guess forces a "0" return code no matter what. When I run the following in the shell, it works fine:

$pgrep -fl ./build-dh
$echo $?
1

So the return value works fine... and when I run this:

 $pgrep -f nginx
11192
11193
11194
11195
11196
$echo $?
0

How can I get this working correctly in PHP?

Thanks

  • 写回答

1条回答 默认 最新

  • dqxboe2628 2012-08-03 22:35
    关注

    Luckily, it does not take much to fix $output and $result from exec to more closely resemble what you get in the CLI:

    foreach($output as $oi=>$o) if(strpos($o,'pgrep')!==false) unset($output[$oi]);
    $return = !count($output); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?