how to kill a process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Rossel

    how to kill a process

    Hi Fellows,
    I have a problem with process termination. I have a python code that
    apache runs through a django interface.
    The code is very simple, first, it creates a process with the
    subprocess.Pope n call, and afterwards, (using a web request) the
    python code uses the PID of the previously created process(stored in a
    db) and kills it with an os.kill call using the SIGKILL signal.

    The creation of the process is ok, apache calls the python code, this
    code creates the process and exits leaving the process up and
    running :)
    But when the python code is called to kill the created process, the
    process is left in a zombie state.

    The kill code that I'm using is:
    os.kill(pid, signal.SIGKILL)

    and I also tried:
    kill_proc = Popen("kill -9 " + pid, shell=true)
    but with no success.

    I suppose that the reason maybe that the python code exits before the
    kill call has finished,
    so I tried with a while loop until kill_proc.poll( ) != None, but
    without success too :(

    do you know what is what I'm doing wrong?

    thanks very much.-

  • Larry Bates

    #2
    Re: how to kill a process

    Richard Rossel wrote:
    Hi Fellows,
    I have a problem with process termination. I have a python code that
    apache runs through a django interface.
    The code is very simple, first, it creates a process with the
    subprocess.Pope n call, and afterwards, (using a web request) the
    python code uses the PID of the previously created process(stored in a
    db) and kills it with an os.kill call using the SIGKILL signal.
    >
    The creation of the process is ok, apache calls the python code, this
    code creates the process and exits leaving the process up and
    running :)
    But when the python code is called to kill the created process, the
    process is left in a zombie state.
    >
    The kill code that I'm using is:
    os.kill(pid, signal.SIGKILL)
    >
    and I also tried:
    kill_proc = Popen("kill -9 " + pid, shell=true)
    but with no success.
    >
    I suppose that the reason maybe that the python code exits before the
    kill call has finished,
    so I tried with a while loop until kill_proc.poll( ) != None, but
    without success too :(
    >
    do you know what is what I'm doing wrong?
    >
    thanks very much.-
    >
    Wouldn't it be better to make the process that you start respond gracefully
    to some signal? Either a command over a socket or some other signal?

    -Larry

    Comment

    • Evan Klitzke

      #3
      Re: how to kill a process

      On 6/12/07, Richard Rossel <henhiskan@gmai l.comwrote:
      But when the python code is called to kill the created process, the
      process is left in a zombie state.
      If the process is left in a zombie state, it's because the parent
      process isn't calling wait(2). If the parent process is your own
      python script, you might try a call to os.wait after the kill
      statement.

      --
      Evan Klitzke <evan@yelp.co m>

      Comment

      • Richard Rossel

        #4
        Re: how to kill a process

        On 12 jun, 13:24, "Evan Klitzke" <e...@yelp.comw rote:
        On 6/12/07, Richard Rossel <henhis...@gmai l.comwrote:
        >
        But when the python code is called to kill the created process, the
        process is left in a zombie state.
        >
        If the process is left in a zombie state, it's because the parent
        process isn't calling wait(2). If the parent process is your own
        python script, you might try a call to os.wait after the kill
        statement.
        >
        The wait call did the trick, but now a sh from kill process
        left in zombie state, so afterwards the waitpid, I added a code line
        to call poll() method from the kill process, and doesn't generates
        zombie
        process anymore :)

        Thanks for your helps


        Comment

        • James T. Dennis

          #5
          Re: how to kill a process

          Richard Rossel <henhiskan@gmai l.comwrote:
          Hi Fellows,
          I have a problem with process termination. I have a python code that
          apache runs through a django interface.
          The code is very simple, first, it creates a process with the
          subprocess.Pope n call, and afterwards, (using a web request) the
          python code uses the PID of the previously created process(stored in a
          db) and kills it with an os.kill call using the SIGKILL signal.
          The creation of the process is ok, apache calls the python code, this
          code creates the process and exits leaving the process up and
          running :)
          But when the python code is called to kill the created process, the
          process is left in a zombie state.
          A zombie is an entry in the process table that stores the exit value
          of a deceased process. (The word is a bit of a misnomer ... and
          the better term would be "death certificate").

          You want to do an os.wait() to clear that entry.

          (The process is well and truly dead after this sort of os.kill()
          but the system still wants the parent process to be able to retrieve
          the exit value and this is the Unix/Linux mechanism for storing that).
          The kill code that I'm using is:
          os.kill(pid, signal.SIGKILL)
          and I also tried:
          kill_proc = Popen("kill -9 " + pid, shell=true)
          but with no success.
          The misunderstandin g here is that you *were* successful.
          You have killed the process. It's dead. Nothing's left but
          a death certificate (or "gravestone " or "corpse" or whatever
          you want to call it). All that remains is for the parent to
          drop by the morgue (the process table) and pick up the remains.
          I suppose that the reason maybe that the python code exits before the
          kill call has finished,
          so I tried with a while loop until kill_proc.poll( ) != None, but
          without success too :(
          do you know what is what I'm doing wrong?
          You are fundamentally misunderstandin g the nature of the process
          table and the meaning of "zombie." Don't feel bad. It's a very
          common misunderstandin g which has sadly been very poorly addressed
          by books on Unix systems administration and programming.
          thanks very much.-
          Glad to help. Try this:

          os.kill(pid, signal.SIGKILL)
          killedpid, stat = os.waitpid(pid, os.WNOHANG)
          if killedpid == 0:
          print >sys.stderr, "ACK! PROCESS NOT KILLED?"

          ... I'm using the "WNOHANG" flag here so that the os.waitpid()
          function will return a tuple of (0,0) if the process isn't
          dead yet. (Shouldn't be possible under these circumstances, but
          understanding how to do this in non-blocking mode is better than
          using the same code pattern in some other case and then being
          surprised, probably unpleasantly, when your process is blocked
          by the call).


          --
          Jim Dennis,
          Starshine: Signed, Sealed, Delivered

          Comment

          Working...