hi ..
im trying to terminate a process
so in python i used

os.system("taskkill /IM notepad.exe/T"
and
os.popen("taskkill /IM notepad.exe /f")
neither have worked since the process does close but start up again !!
on SOME computers it did work, the process was terminated
but on others it did not

what would be a good way to stop this process from running and stop whatever is running it running !?

i need to terminate this process so it does not start again and any affect that it cause the computer to have will be gone

i can find the pid value and the name of the image
using
os.system(tasklist)

so all i need is a way to kill the process
sorry for my english, im not a native english speaker
Thanks
DDNDD

Hi! Have you looked at using os.kill()?

Hi! Have you looked at using os.kill()?

yes but im not really sure on how to use that
can you please give an example ??

A list of old recipes.

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

# windows
handle = subprocess.Popen("someprocess here", shell=False)
subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)

#also
# Create a process that won't end on its own
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])


# Kill the process using pywin32
import win32api
win32api.TerminateProcess(int(process._handle), -1)


# Kill the process using ctypes
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)


# Kill the proces using pywin32 and pid
import win32api
PROCESS_TERMINATE = 1
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)


# Kill the proces using ctypes and pid
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
commented: Santa python.... Keep it up buddy +2
commented: thanks +13

A list of old recipes.

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

# windows
handle = subprocess.Popen("someprocess here", shell=False)
subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)

#also
# Create a process that won't end on its own
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])


# Kill the process using pywin32
import win32api
win32api.TerminateProcess(int(process._handle), -1)


# Kill the process using ctypes
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)


# Kill the proces using pywin32 and pid
import win32api
PROCESS_TERMINATE = 1
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)


# Kill the proces using ctypes and pid
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)

im using windows xp most of the time
i have not tried this yet

# windows
handle = subprocess.Popen("someprocess here", shell=False)
subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)

im going to but do you think if i terminate the process using it would the process show up again ?? because that is the problem

im using windows xp most of the time
i have not tried this yet

# windows
handle = subprocess.Popen("someprocess here", shell=False)
subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)

im going to but do you think if i terminate the process using it would the process show up again ?? because that is the problem

I don't know why your process shows up again, so try the recipes one after the other. If the other process was started from your python program with proc = subprocess.Popen(...) , you can also use proc.terminate() in recent versions of python. My second choice would be the method with ctypes.

Grib

You eat and drink python..!
What a man! ;)

commented: I love Grib, he's amazing! =3 +4

Great info, thanks for sharing! I'm a python noob, but I'm learing a lot just watching :) I was having trouble figuring out how to get the pid in Windows if the process wasn't spawned by the python script (I don't spend a lot of time in Windows)

:)

Google led me to wmi http://timgolden.me.uk/python/wmi/cookbook.html#list-all-running-processes which can help you explore running processes in windows. There are also old snippets http://code.activestate.com/recipes/303339-getting-process-information-on-windows/ and http://code.activestate.com/recipes/305279-getting-process-information-on-windows/.

I did not test all this myself, because I don't usually work on windows ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.