2

I'm running calc.exe (windows 10, python 3.6) using proc = subprocess.Popen("calc.exe"), then time.sleep(time) and then want to kill process: os.kill(proc.pid, -9) or os.kill(proc.pid, signal.SIGTERM) gives me error

"Access Denied".

I also tried proc.terminate - it didn't help.

And I also noticed that proc.pid gives different PID from PID which is shown in task manager. Any ideas how to kill my process?

Thanks!

Albert
  • 21
  • 1
  • 3
  • 1
    Possible duplicate of [Terminate subprocess in Windows, access denied](https://stackoverflow.com/questions/2868129/terminate-subprocess-in-windows-access-denied) – Bacon Jul 13 '18 at 13:18

2 Answers2

1

You can try using windows to kill the process.

command = "Taskkill /IM calc.exe /F"
proc = subprocess.Popen(command)

or

import os
os.system("taskkill /im Calculator.exe /f")

If you want to be sure., Try a recursive kill!!

def kill_process(proc):
    # Check process is running, Kill it if it is,

    # Try to kill the process - 0 exit code == success

    kill = "TaskKill /IM {} /F".format(proc)

    res = subprocess.run(kill)

    if res == 0:
        return True  # Process Killed
    else:
        kill_process(proc)  # Process not killed repeat until it is!

kill_process('Calculator.exe')
johnashu
  • 2,167
  • 4
  • 19
  • 44
  • OK, thanks! I'll try it on Monday and will write you my result. P.S, i've just tried to execute the code i've written on Windows 7 with proc.terminate() and it works! Do you know why it doesn't on Win10? – Albert Jul 13 '18 at 13:07
  • Probably something to do with user access rights..Maybe try running the script as `admin`. I had the same problem on windows 10 and had to write this function that does the job.. – johnashu Jul 13 '18 at 13:10
  • It didn't help. A saw in the Task Manager that process was created with the name "Calculator.exe" (not calc.exe). I tried to implement "proc.terminate()" once more - it workswithout errors but when script terminates, calculator is still working:( – Albert Jul 16 '18 at 05:58
  • What happens if you run `TaskKill /IM calc.exe /F` from cmd? (Also try Calculator.exe) – johnashu Jul 16 '18 at 06:41
  • Calculator.exe - terminates successfully, calc.exe gives error: windows can't find such process – Albert Jul 16 '18 at 06:55
  • if you try `Calculator.exe` in the python code? what happens? – johnashu Jul 16 '18 at 06:57
  • it terminates successfully too. And if i execute proc.pid, it gives (for example) 7740 and then when execute os.system("taskkill ...") cmd prints "Process with PID 6612 was terminated" - PID's are different, that's why i think proc.terminate doesn't work :( But how can I get the value 6612 when execute Popen("calc.exe")? – Albert Jul 16 '18 at 07:05
  • it looks like I execute "calc.exe" - standart windows's application but it creates "Calculator.exe" and then I know nothing about PID of the "Calculator.exe" from my proc variable – Albert Jul 16 '18 at 07:58
  • Have you any ideas? – Albert Jul 16 '18 at 08:50
1

In the python 3 subprocess API, one can kill the child by calling

Popen.kill()

which is an alias for Popen.terminate() in Windows (see here). If this doesn't work, you can try

os.system("TASKKILL /F /PID [child PID]")

You can get the PID of the child with Popen.pid()

Bacon
  • 1,814
  • 3
  • 21
  • 36