1

I am trying to kill a process on windows with its PID using

os.kill(pid, 9)

This generally works. Sometimes, however, the process has crashed due to an OutOfMemoryError exception while it was running. This makes the entire UI of the process freeze, it cannot be interacted with anymore, and if I send the kill signal to the process the os.kill function is blocking and nothing happens.

If the process has crashed in this way, I cannot close it through the regular windows GUI either. But if I open the task manager, look for the process in the 'Details' tab, and select 'End Task' it closes right away without issues.

Is there any way to emulate this task manager function through Python code if the process cannot receive any signals due to an error that could not be handled?

J. Grohmann
  • 391
  • 1
  • 6
  • 15

1 Answers1

1

If you want to simulate the ending process, you only need to use os.system:

import os
os.system("taskkill /F /T /IM MyProcess.exe")
Zeus
  • 3,703
  • 3
  • 7
  • 20