subprocess and PPID

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Petrazzo

    subprocess and PPID

    Hi all,
    I believe that this is a *nix question, but since I'm developing in
    python, I'm here.

    I have a code that execute into a "Popen" a command (ssh). I need that,
    if the python process die, the parent pid (PPID) of the child don't
    become 1 (like I can seen on /proc/$pid$/status ), but it has to die,
    following it's parent
    It's possible in linux and with subprocess?

    Thanks,
    Michele
  • saju.pillai@gmail.com

    #2
    Re: subprocess and PPID

    On Nov 5, 5:12 pm, Michele Petrazzo <michele.petra. ..@TOGLIunipex. it>
    wrote:
    Hi all,
    I believe that this is a *nix question, but since I'm developing in
    python, I'm here.
    >
    I have a code that execute into a "Popen" a command (ssh). I need that,
    if the python process die, the parent pid (PPID) of the child don't
    become 1 (like I can seen on /proc/$pid$/status ), but it has to die,
    following it's parent
    It's possible in linux and with subprocess?

    AFAIK, there is no easy way to do this. If the parent python process
    is doing a controlled exit, just kill the child via close() on Popen()
    handle. If the parent is doing a uncontrolled exit (say via a SIGKILL
    signal), you can't really do anything.

    To reliably have the child exit when the parent exits, you would have
    to poll for the parent from the child and do a exit when the child
    detects that the parent has gone away.

    -srp
    >
    Thanks,
    Michele

    Comment

    • Jorgen Grahn

      #3
      Re: subprocess and PPID

      On Wed, 5 Nov 2008 08:19:34 -0800 (PST), saju.pillai@gma il.com <saju.pillai@gm ail.comwrote:
      On Nov 5, 5:12 pm, Michele Petrazzo <michele.petra. ..@TOGLIunipex. it>
      wrote:
      >Hi all,
      >I believe that this is a *nix question, but since I'm developing in
      >python, I'm here.
      >>
      >I have a code that execute into a "Popen" a command (ssh). I need that,
      What's 'a "Popen"'? Is it os.popen or one of its variants?

      Do you read from it or write to it? If you do both, you should use
      something like the third-party module pexpect instead.
      >if the python process die, the parent pid (PPID) of the child don't
      >become 1 (like I can seen on /proc/$pid$/status ), but it has to die,
      >following it's parent
      >It's possible in linux and with subprocess?
      >
      AFAIK, there is no easy way to do this. If the parent python process
      is doing a controlled exit, just kill the child via close() on Popen()
      handle. If the parent is doing a uncontrolled exit (say via a SIGKILL
      signal), you can't really do anything.
      >
      To reliably have the child exit when the parent exits, you would have
      to poll for the parent from the child and do a exit when the child
      detects that the parent has gone away.
      But in the special case where he's feeding data into "ssh somewhere
      somecmd" or pulling data from it, a crash of the parent should make
      "somecmd" exit because it sees EOF, and thus make the ssh process end.
      Sounds relatively risk-free -- but it depends on "somecmd".

      /Jorgen

      --
      // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
      \X/ snipabacken.se R'lyeh wgah'nagl fhtagn!

      Comment

      • Lawrence D'Oliveiro

        #4
        Re: subprocess and PPID

        In message <ges2jc$202$1@n nrp-beta.newsland.i t>, Michele Petrazzo wrote:
        I have a code that execute into a "Popen" a command (ssh). I need that,
        if the python process die, the parent pid (PPID) of the child don't
        become 1 (like I can seen on /proc/$pid$/status ), but it has to die,
        following it's parent
        It's possible in linux and with subprocess?
        There is a Linux-specific system call that says it does this (haven't
        tried). See the prctl(2) man page.

        Comment

        • Michele Petrazzo

          #5
          Re: subprocess and PPID

          Jorgen Grahn wrote:
          On Wed, 5 Nov 2008 08:19:34 -0800 (PST), saju.pillai@gma il.com
          <saju.pillai@gm ail.comwrote:
          >On Nov 5, 5:12 pm, Michele Petrazzo
          ><michele.petra ...@TOGLIunipex .itwrote:
          >>Hi all, I believe that this is a *nix question, but since I'm
          >>developing in python, I'm here.
          >>>
          >>I have a code that execute into a "Popen" a command (ssh). I need
          >>that,
          >
          What's 'a "Popen"'? Is it os.popen or one of its variants?
          >
          Popen is the default python Popen:

          from subprocess import Popen, PIPE

          cmd = "ssh -C -N -i keys/id_rsa_key -L remote:ip:local who@where"

          cmd_p = Popen(cmd.split (), stdout=PIPE, stderr=PIPE)

          Do you read from it or write to it? If you do both, you should use
          something like the third-party module pexpect instead.
          >
          Nothing. I do only a tunnel
          >To reliably have the child exit when the parent exits, you would
          >have to poll for the parent from the child and do a exit when the
          >child detects that the parent has gone away.
          >
          But in the special case where he's feeding data into "ssh somewhere
          somecmd" or pulling data from it, a crash of the parent should make
          "somecmd" exit because it sees EOF, and thus make the ssh process
          end. Sounds relatively risk-free -- but it depends on "somecmd".
          >
          Interesting. So, how I have to modify my code (if I can)? Add an stdin?
          /Jorgen
          >
          Thanks,
          Michele

          Comment

          • Michele Petrazzo

            #6
            Re: subprocess and PPID

            Lawrence D'Oliveiro wrote:
            In message <ges2jc$202$1@n nrp-beta.newsland.i t>, Michele Petrazzo
            wrote:
            >
            >I have a code that execute into a "Popen" a command (ssh). I need
            >that, if the python process die, the parent pid (PPID) of the child
            >don't become 1 (like I can seen on /proc/$pid$/status ), but it has
            >to die, following it's parent It's possible in linux and with
            >subprocess?
            >
            There is a Linux-specific system call that says it does this (haven't
            tried). See the prctl(2) man page.
            Just seen. It can be, bust since I cannot modify the child process and
            this syscall must be called from the child, I cannot use it.

            Thanks,
            Michele

            Comment

            • Michele Petrazzo

              #7
              Re: subprocess and PPID

              saju.pillai@gma il.com wrote:
              >It's possible in linux and with subprocess?
              >
              >
              AFAIK, there is no easy way to do this. If the parent python process
              is doing a controlled exit, just kill the child via close() on
              Popen() handle.
              Like I do ;)
              If the parent is doing a uncontrolled exit (say via a SIGKILL
              signal), you can't really do anything.
              >
              The only thing that I thought it's to use an external resource, like a
              ..pid file where I save the child pid(s) and, on the next parent startup
              control the file and kill the process, if any
              To reliably have the child exit when the parent exits, you would have
              to poll for the parent from the child and do a exit when the child
              detects that the parent has gone away.
              >
              Like said, I haven't the control of the sources, so I can't.

              Thanks,
              Michele

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: subprocess and PPID

                In message <geu8k4$ceo$1@n nrp-beta.newsland.i t>, Michele Petrazzo wrote:
                Lawrence D'Oliveiro wrote:
                >
                >See the prctl(2) man page.
                >
                Just seen. It can be, bust since I cannot modify the child process and
                this syscall must be called from the child, I cannot use it.
                You do the fork and then the exec, right? So do the prctl in-between.

                Comment

                • saju.pillai@gmail.com

                  #9
                  Re: subprocess and PPID

                  On Nov 6, 3:09 pm, Lawrence D'Oliveiro <l...@geek-
                  central.gen.new _zealandwrote:
                  In message <geu8k4$ce...@n nrp-beta.newsland.i t>, Michele Petrazzo wrote:
                  >
                  Lawrence D'Oliveiro wrote:
                  >
                  See the prctl(2) man page.
                  >
                  Just seen. It can be, bust since I cannot modify the child process and
                  this syscall must be called from the child, I cannot use it.
                  >
                  You do the fork and then the exec, right? So do the prctl in-between.
                  You could also write a wrapper program that does a prctl and then
                  exec(actual command). Infact you could use a wrapper program to
                  portably poll for the parent if you dont want to prctl(); invoke this
                  wrapper from python, the wrapper can then invoke your actual command.

                  -srp

                  Comment

                  Working...