How to close a program I execute with subprocess.Popen?

R

revenant81

I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
I want to end the execution of the command when the user closes my
application.

Right now I'm using an object my_child of type subprocess.Popen to
execute the command, inside a thread with an infinite loop where we
constantly ask for its output.

To end the program when the user closes the application, I send a
SIGTERM to the process with pid my_child.pid using os.kill. But I also
have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the
pid of /bin/sh -c which is the one which calls the command, because
when I try to run Popen with shell=False, it sends an exception and
says the file or directory doesn't exist.

Anyone knows of a better way to close the command than using a
SIGTERM? I just can't help myself thinking this is an ugly dirty hack.
 
A

A.T.Hofkamp

I want to end the execution of the command when the user closes my
application.

Right now I'm using an object my_child of type subprocess.Popen to
execute the command, inside a thread with an infinite loop where we
constantly ask for its output.

To end the program when the user closes the application, I send a
SIGTERM to the process with pid my_child.pid using os.kill. But I also
have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the

Maybe that holds for a system recently started and mostly idle, but it need
not be the case. If you have more users active, and each user is forking
processes, (or one user is forking several processes concurrently), the order
of assigning process IDs is not defined.
For systems that run for a longer time, unused process IDs are re-used, skipping
over process IDs that are still living.
pid of /bin/sh -c which is the one which calls the command, because
when I try to run Popen with shell=False, it sends an exception and
says the file or directory doesn't exist.

The shell performs a search over PATH to find the command executable (ie it
maps 'ls' to '/bin/ls'). If you provide a full path to the command you are
starting, you don't need the shell.
Anyone knows of a better way to close the command than using a
SIGTERM? I just can't help myself thinking this is an ugly dirty hack.

In principle, you should only kill your own child processes, your child process
should handle its own childs (your grant child processes). SIGTERM is one way.
Another solution often adopted is to close the stdin of the child. This
notifies the child that no more data will arrive, and many commands react on
that message by terminating. Try running

command < /dev/null

/dev/null is the empty input stream.

Last but not least, many commands do something special when sent -HUP. Look in
the manual page of the command for clean ways to close the command down.


Albert
 
N

Nick Craig-Wood

A.T.Hofkamp said:
In principle, you should only kill your own child processes, your child process
should handle its own childs (your grant child processes). SIGTERM is one way.
Another solution often adopted is to close the stdin of the child.

That is a good idea.

You could also make the parent a process group leader and kill the
process group. You'll get the kill signal too which you'll need to
ignore. That is how the shell keeps track of things IIRC.

Read man setpgid, getpgid should be helful. There are equivalent
commands in os, ie os.setpgid and os.getpgid.
 
J

jitudon

I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
I want to end the execution of the command when the user closes my
application.

Right now I'm using an object my_child of type subprocess.Popen to
execute the command, inside a thread with an infinite loop where we
constantly ask for its output.

To end the program when the user closes the application, I send a
SIGTERM to the process with pid my_child.pid using os.kill. But I also
have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the
pid of /bin/sh -c which is the one which calls the command, because
when I try to run Popen with shell=False, it sends an exception and
says the file or directory doesn't exist.

Anyone knows of a better way to close the command than using a
SIGTERM? I just can't help myself thinking this is an ugly dirty hack.

As nick pointed out use process group's .
I use the "preexec_fn" keyword argument to Popen and "os.setsid()"
call's side effect
to make process groups and then os.killpg() to send the signal to
process groups.

child = Popen( cmd , preexec_fn = os.setsid )
os.killpg( child.pid,signal.SIGINT)

Regards
jitu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top