Kill processes open with popen ()?

I open a lengthy process with popen (). For debugging, I would like to complete the process before it is complete. The call to pclose () simply blocks until the child completes execution.

How can I kill the process? I don't see any easy way to get the pid from the resource that popen () returns so that I can send it a signal.

I suppose I could do something kludgey and try to get the pid out, using some kind of command line hacking ...

+6
linux php popen kill
source share
3 answers

Well, after landing on the solution: I switched to proc_open() instead of popen() . Then it is as simple as:

 $s = proc_get_status($p); posix_kill($s['pid'], SIGKILL); proc_close($p); 
+7
source share

Just send the kill signal (or abort) with the kill function:

0
source share

You can find the pid and check if you are really its parent:

 // Find child processes according to current pid $res = trim(exec('ps -eo pid,ppid |grep "'.getmypid().'" |head -n2 |tail -n1')); if (preg_match('~^(\d+)\s+(\d+)$~', $res, $pid) !== 0 && (int) $pid[2] === getmypid()) { // I'm the parent PID, just send a KILL posix_kill((int) $pid[1], 9); } 

It works fine on the fast-cgi PHP server.

0
source share

All Articles