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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • revenant81@hotmail.com

    #1

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

    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.Pope n 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.

  • Nick Craig-Wood

    #2
    Re: How to close a program I execute with subprocess.Pope n?

    A.T.Hofkamp <hat@se-162.se.wtb.tue. nlwrote:
    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 <nick@craig-wood.com-- http://www.craig-wood.com/nick

    Comment

    • jitudon@hotmail.com

      #3
      Re: How to close a program I execute with subprocess.Pope n?

      On Jun 29, 6:36 pm, revenan...@hotm ail.com 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.Pope n 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,signa l.SIGINT)

      Regards
      jitu




      Comment

      Working...