end child process when parent dies (on Unix)

Discussion in 'Python' started by Дамјан ГеоргиевÑки, Nov 17, 2008.

  1. Hi all,
    I'm starting a Unix tool with subprocess.Popen() from a python script
    and I want the child to be killed when the parent (my script) ends for
    whatever reason *including* if it gets killed by SIGKILL.

    For normal situations I can send a signal to the pid of the Popen object.
    But not if the script ends abnormally (SIGKILL etc).

    AFAIK there were some tricks with process leaders and stuff like that so if
    someone remembers the exact semantics of that could refresh my memory.



    --
    дамјан ( http://softver.org.mk/damjan/ )

    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Дамјан ГеоргиевÑки, Nov 17, 2008
    #1
    1. Advertising

  2. Дамјан ГеоргиевÑки wrote:

    > I'm starting a Unix tool with subprocess.Popen() from a python script
    > and I want the child to be killed when the parent (my script) ends for
    > whatever reason *including* if it gets killed by SIGKILL.


    A Linux-specific solution is prctl(2).
    Lawrence D'Oliveiro, Nov 17, 2008
    #2
    1. Advertising

  3. >> I'm starting a Unix tool with subprocess.Popen() from a python script
    >> and I want the child to be killed when the parent (my script) ends
    >> for whatever reason *including* if it gets killed by SIGKILL.

    >
    > A Linux-specific solution is prctl(2).


    I've tried this in a test C program... exactly what I need. Now if I
    could slip this between the fork and exec in subprocess.Popen()


    --
    дамјан ( http://softver.org.mk/damjan/ )

    In theory, there is no difference between theory and practice.
    But, in practice, there is.
    Дамјан ГеоргиевÑки, Nov 17, 2008
    #3
  4. Дамјан ГеоргиевÑки wrote:

    >>> I'm starting a Unix tool with subprocess.Popen() from a python script
    >>> and I want the child to be killed when the parent (my script) ends
    >>> for whatever reason *including* if it gets killed by SIGKILL.

    >>
    >> A Linux-specific solution is prctl(2).

    >
    > I've tried this in a test C program... exactly what I need. Now if I
    > could slip this between the fork and exec in subprocess.Popen()


    An obvious way, if you're trying to hack something without the source, is to substitute your own executable which does the prctl before execing the real executable.
    Lawrence D'Oliveiro, Nov 17, 2008
    #4
  5. Дамјан ГеоргиевÑки

    Miles Guest

    On Mon, Nov 17, 2008 at 2:51 AM, Дамјан ГеоргиевÑки wrote:
    >>> I'm starting a Unix tool with subprocess.Popen() from a python script
    >>> and I want the child to be killed when the parent (my script) ends
    >>> for whatever reason *including* if it gets killed by SIGKILL.

    >>
    >> A Linux-specific solution is prctl(2).

    >
    > I've tried this in a test C program... exactly what I need. Now if I
    > could slip this between the fork and exec in subprocess.Popen()


    preexec_fn, perhaps?

    http://docs.python.org/library/subprocess.html#using-the-subprocess-module

    -Miles
    Miles, Nov 18, 2008
    #5
    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. rp
    Replies:
    4
    Views:
    521
    John C. Bollinger
    Nov 24, 2003
  2. Noel Dolan
    Replies:
    0
    Views:
    216
    Noel Dolan
    Jul 18, 2004
  3. Christopher M. Lusardi
    Replies:
    3
    Views:
    179
    Alan J. Flavell
    Oct 19, 2003
  4. Paul Clements
    Replies:
    2
    Views:
    192
    Ben Morrow
    Feb 11, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page