How would I go about killing a process on Windows?
I am starting the process with
self.p = Process(target=self.GameInitialize, args=(testProcess,))
self.p.start()
I have tried
self.p.kill()
self.p.terminate()
os.kill(self.p.pid, -1)
os.killpg(self.p.pid, signal.SIGTERM) # Send the signal to all the process groups
Errors
Process Object has no Attribute kill
Process Object has no Attribute terminate
Access Denied
I cannot use .join.
Use subprocess. Popen. To terminate the subprocess, call subprocess. Popen. terminate() with subprocess.
You can also create a batch file called kill. bat with this command and place it in a folder that is seen as an eviromental variable so that you just can write the command kill to eliminate all the running threads of the python running scripts.
You can kill a process via its process identifier, pid, via the os. kill() function.
Since subprocess. call waits for the command to complete, you can't kill it programmatically. Your only recourse is to kill it manually via an OS specific command like kill .
On windows, os.killpg
will not work because it sends a signal to the process ID to terminate. This is now how you kill a process on Windows, instead you have to use the win32 API's TerminateProcess
to kill a process.
So, you can kill a process by the following on windows:
import signal
os.kill(self.p.pid, signal.CTRL_C_EVENT)
If the above does not work, then try signal.CTRL_BREAK_EVENT
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With