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

Discussion in 'Python' started by revenant81@hotmail.com, Jun 29, 2007.

  1. Guest

    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.
     
    , Jun 29, 2007
    #1
    1. Advertising

  2. A.T.Hofkamp Guest

    On 2007-06-29, <> wrote:
    > 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
     
    A.T.Hofkamp, Jun 29, 2007
    #2
    1. Advertising

  3. A.T.Hofkamp <> wrote:
    > 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.

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Jul 1, 2007
    #3
  4. Guest

    On Jun 29, 6:36 pm, wrote:
    > 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
     
    , Jul 6, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    767
  2. Steven K. Wong
    Replies:
    6
    Views:
    1,090
    Nobody
    Jan 10, 2010
  3. Iñaki Baz Castillo
    Replies:
    7
    Views:
    938
    Iñaki Baz Castillo
    Jan 12, 2010
  4. Niklas Berliner
    Replies:
    0
    Views:
    177
    Niklas Berliner
    Jan 10, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    156
    Dave Angel
    Jan 10, 2013
Loading...

Share This Page