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
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);