Welcome! Log In Create A New Profile

Advanced

[PHP] proc_get_status() not returning correct pid

Posted by James Colannino 
James Colannino
[PHP] proc_get_status() not returning correct pid
August 02, 2017 09:00PM
Hi everyone,

It seems proc_get_status() isn't returning the correct pid when I spawn a
background php process using proc_open (or at least not the pid I'm
expecting.) I'm calling it like this (I'll ommit the descriptorspec and
other parameters for brevity's sake):

$this->process = proc_open(
'php worker.php',
$descriptorspec,
$pipes,
$cwd,
$env
);

Later, if I var_dump the output of proc_get_status($this->process), I'll
get something like this:

array(8) {
["command"]=>
string(14) "php worker.php"
["pid"]=>
int(24173)
["running"]=>
bool(true)
["signaled"]=>
bool(false)
["stopped"]=>
bool(false)
["exitcode"]=>
int(-1)
["termsig"]=>
int(0)
["stopsig"]=>
int(0)
}

However, when I check on the system as it's running, I see that the above
pid doesn't belong to the executing php process, but rather to a process
with the command "sh -c php worker.php." As a result, when I try to kill
the process, I still have processes with the command "php worker.php" left
behind (these are backgrounded consumers that are waiting on input they'll
never receive.)

This is the output of ps aux | grep php as an example:

user+ 24173 0.0 0.0 4508 764 pts/0 S+ 18:51 0:00 sh -c php
worker.php
user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php worker.php

After running either proc_terminate($this-process) or
posix_kill(proc_get_status($this->process)['pid'], SIGKILL), I still have
the following left behind:

user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php worker.php

Only the "sh -c php worker.php" process disappears.

Does anyone have an idea of what's going on and how I can get the pid for
the correct process so I can kill it from inside PHP?

Thanks!

This is on PHP 7.0.21.
Aziz Saleh
Re: [PHP] proc_get_status() not returning correct pid
August 02, 2017 09:20PM
On Wed, Aug 2, 2017 at 2:52 PM, James Colannino <[email protected]>
wrote:

> Hi everyone,
>
> It seems proc_get_status() isn't returning the correct pid when I spawn a
> background php process using proc_open (or at least not the pid I'm
> expecting.) I'm calling it like this (I'll ommit the descriptorspec and
> other parameters for brevity's sake):
>
> $this->process = proc_open(
> 'php worker.php',
> $descriptorspec,
> $pipes,
> $cwd,
> $env
> );
>
> Later, if I var_dump the output of proc_get_status($this->process), I'll
> get something like this:
>
> array(8) {
> ["command"]=>
> string(14) "php worker.php"
> ["pid"]=>
> int(24173)
> ["running"]=>
> bool(true)
> ["signaled"]=>
> bool(false)
> ["stopped"]=>
> bool(false)
> ["exitcode"]=>
> int(-1)
> ["termsig"]=>
> int(0)
> ["stopsig"]=>
> int(0)
> }
>
> However, when I check on the system as it's running, I see that the above
> pid doesn't belong to the executing php process, but rather to a process
> with the command "sh -c php worker.php." As a result, when I try to kill
> the process, I still have processes with the command "php worker.php" left
> behind (these are backgrounded consumers that are waiting on input they'll
> never receive.)
>
> This is the output of ps aux | grep php as an example:
>
> user+ 24173 0.0 0.0 4508 764 pts/0 S+ 18:51 0:00 sh -c php
> worker.php
> user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php
> worker.php
>
> After running either proc_terminate($this-process) or
> posix_kill(proc_get_status($this->process)['pid'], SIGKILL), I still have
> the following left behind:
>
> user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php
> worker.php
>
> Only the "sh -c php worker.php" process disappears.
>
> Does anyone have an idea of what's going on and how I can get the pid for
> the correct process so I can kill it from inside PHP?
>
> Thanks!
>
> This is on PHP 7.0.21.
>

Check out the first comment on
http://php.net/manual/en/function.proc-terminate.php. Seem to have had the
same issue as you, possibly!
James Colannino
Re: [PHP] proc_get_status() not returning correct pid
August 02, 2017 11:40PM
Yes, I missed that. That solved my problem. Thank you! :)

On Wed, Aug 2, 2017 at 12:14 PM, Aziz Saleh <[email protected]> wrote:

>
>
> On Wed, Aug 2, 2017 at 2:52 PM, James Colannino <[email protected]>
> wrote:
>
>> Hi everyone,
>>
>> It seems proc_get_status() isn't returning the correct pid when I spawn a
>> background php process using proc_open (or at least not the pid I'm
>> expecting.) I'm calling it like this (I'll ommit the descriptorspec and
>> other parameters for brevity's sake):
>>
>> $this->process = proc_open(
>> 'php worker.php',
>> $descriptorspec,
>> $pipes,
>> $cwd,
>> $env
>> );
>>
>> Later, if I var_dump the output of proc_get_status($this->process), I'll
>> get something like this:
>>
>> array(8) {
>> ["command"]=>
>> string(14) "php worker.php"
>> ["pid"]=>
>> int(24173)
>> ["running"]=>
>> bool(true)
>> ["signaled"]=>
>> bool(false)
>> ["stopped"]=>
>> bool(false)
>> ["exitcode"]=>
>> int(-1)
>> ["termsig"]=>
>> int(0)
>> ["stopsig"]=>
>> int(0)
>> }
>>
>> However, when I check on the system as it's running, I see that the above
>> pid doesn't belong to the executing php process, but rather to a process
>> with the command "sh -c php worker.php." As a result, when I try to kill
>> the process, I still have processes with the command "php worker.php" left
>> behind (these are backgrounded consumers that are waiting on input they'll
>> never receive.)
>>
>> This is the output of ps aux | grep php as an example:
>>
>> user+ 24173 0.0 0.0 4508 764 pts/0 S+ 18:51 0:00 sh -c php
>> worker.php
>> user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php
>> worker.php
>>
>> After running either proc_terminate($this-process) or
>> posix_kill(proc_get_status($this->process)['pid'], SIGKILL), I still have
>> the following left behind:
>>
>> user+ 24174 0.0 0.7 312180 31544 pts/0 S+ 18:51 0:00 php
>> worker.php
>>
>> Only the "sh -c php worker.php" process disappears.
>>
>> Does anyone have an idea of what's going on and how I can get the pid for
>> the correct process so I can kill it from inside PHP?
>>
>> Thanks!
>>
>> This is on PHP 7.0.21.
>>
>
> Check out the first comment on http://php.net/manual/en/
> function.proc-terminate.php. Seem to have had the same issue as you,
> possibly!
>
>
Sorry, only registered users may post in this forum.

Click here to login