Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing processes opened with popen()?

I'm opening a long-running process with popen(). For debugging, I'd like to terminate the process before it has completed. Calling pclose() just blocks until the child completes.

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

I suppose I could do something kludgey and try to fudge the pid into the output using some sort of command-line hackery...

like image 967
Frank Farmer Avatar asked Jan 19 '11 04:01

Frank Farmer


People also ask

How do I kill a process launched by Popen?

Both kill or terminate are methods of the Popen object. On macOS and Linux, kill sends the signal signal. SIGKILL to the process and terminate sends signal. SIGTERM .

How do you kill a Popen process in Python?

The subprocess. Popen() is a built-in Python function that executes a child program in a new process. In this example, we saw how to kill all the subprocesses using the killpg command. The killpg() method send the signal to all the process group to terminate the process.

Does Popen wait for process to finish?

Using Popen MethodThe Popen method does not wait to complete the process to return a value. This means that the information you get back will not be complete.

What is subprocess popen ()?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.


1 Answers

Well, landed on a solution: I switched back to proc_open() instead of popen(). Then it's as simple as:

$s = proc_get_status($p);
posix_kill($s['pid'], SIGKILL);
proc_close($p);
like image 138
Frank Farmer Avatar answered Sep 23 '22 10:09

Frank Farmer