83

Here is my thought:

First of all, I created a process by using subprocess.Popen

Second, after certain amount of time, I tried to kill it by Popen.kill()

import subprocess
import os, signal
import time

proc1 = subprocess.Popen("kvm -hda /path/xp.img", shell = True)
time.sleep(2.0)
print 'proc1 = ', proc1.pid
subprocess.Popen.kill(proc1)

However, "proc1" still exists after Popen.kill(). Could any experts tell me how to solve this issue? I appreciate your considerations.

Thanks to the comments from all experts, I did all you recommended, but result still remains the same.

proc1.kill() #it sill cannot kill the proc1

os.kill(proc1.pid, signal.SIGKILL) # either cannot kill the proc1

Thank you all the same.

And I am still waiting for your precious experience on solving this delicate issue.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
user495511
  • 831
  • 1
  • 6
  • 5
  • Why do you need the "shell = True"? Without this it seems to work exactly how you would expect. – Michael Patterson Nov 04 '10 at 05:16
  • 4
    Instead of putting [Solved] in the title, please choose the correct answer clicking on the check symbol. – bluish Dec 20 '10 at 16:41
  • 2
    proper answer is here: http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true – Sam Watkins Jan 11 '13 at 06:12
  • `subprocess.Popen.kill(proc)` is exactly the same as `proc.kill()` FYI. – Erik Kaplun Mar 19 '14 at 06:19
  • 1
    it is unlikely that [`kill -9` doesn't work](http://unix.stackexchange.com/q/5642/1321). Your issue could be that you need to [kill the whole process tree](http://stackoverflow.com/q/392022/4279). – jfs Jan 23 '15 at 15:05

4 Answers4

52

In your code it should be

proc1.kill()

Both kill or terminate are methods of the Popen object. On macOS and Linux, kill sends the signal signal.SIGKILL to the process and terminate sends signal.SIGTERM. On Windows they both call Windows' TerminateProcess() function.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 25
    Thanks first. However, I tried this one, it still does not work... – user495511 Nov 03 '10 at 13:07
  • 5
    `kill()` and `terminate()` are different The kill method sends a SIGKILL signal, where the terminate method sends a SEGTERM signal. The difference is that the former it literally terminate the process where the latter it only stops the process. – Mohamed Horani Apr 14 '18 at 02:20
40

process.terminate() doesn't work when using shell=True. This answer will help you.

Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
metanoia5
  • 553
  • 5
  • 8
13

Only use Popen kill method

process = subprocess.Popen(
    task.getExecutable(), 
    stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE, 
    shell=True
)
process.kill()
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
0

How about using os.kill? See the docs here: http://docs.python.org/library/os.html#os.kill

Michael Patterson
  • 1,750
  • 13
  • 11
  • 6
    Thanks, but "os.kill(proc1.pid, signal.SIGTERM)" or "os.kill(proc1.pid, signal.SIGKILL)" are not working.. – user495511 Nov 03 '10 at 13:11