8

I've been trying to kill a process but all my options give me a Windows Access Denied Error. I open the process(a python script) through test= subprocess.Popen([sys.executable, "testsc.py"]) and I want to kill that process.

So far, I've tried os.kill(pid, signal.SIGILL) , os.kill(pid, 9), test.Terminate() and simply test.kill(). All of these give me the error.

I am using Python 2.7.1.4 on a Windows 7 x86 machine. I would appreciate the help! Thanks!

mac
  • 42,153
  • 26
  • 121
  • 131
dawnoflife
  • 1,572
  • 10
  • 42
  • 62
  • 1
    We need more code. Maybe the process is already done when you are trying to kill it. – Jacob Jul 14 '11 at 05:40
  • There seem to be a number of such reports, e.g. [this one with the same title minus a colon and with a lowercase E](http://stackoverflow.com/questions/3005437/windowserror-error-5-access-is-denied) and [this one with a decent name ("Terminate subprocess in Windows, access denied")](http://stackoverflow.com/questions/2868129/terminate-subprocess-in-windows-access-denied). – Chris Morgan Jul 14 '11 at 05:45
  • Observing the documentation for [os.kill](http://docs.python.org/library/os.html#os.kill), "New in version 2.7: Windows support". But you say you're using Python 2.7 so that shouldn't be a problem. – Chris Morgan Jul 14 '11 at 05:55
  • What process are you trying to kill? Did you spawn it? If it's a SYSTEM process, you won't be able to kill it as a local admin. – Josh Jul 14 '11 at 06:14
  • 1
    @Josh: did you *read*? `subprocess.Popen([sys.executable, "testsc.py"])`. He should be able to kill that! – Chris Morgan Jul 14 '11 at 08:27
  • @Chris my mistake - for some reason I had it in my head he was attaching to the process instead of spawning it, idk what I was thinking. – Josh Jul 14 '11 at 14:59

3 Answers3

3

A workaround if anyone interested - even as an admin I get access denied on some services when using os.kill. however, this works:

import subprocess
subprocess.check_output("Taskkill /PID %d /F" % pid)

So if you don't care about being cross-platform and want a quick and dirty solution - try this instead.

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50
0

Okey, so i was having the same problem, that you have + having a problem with some annoying api i tought "Well, there is no chance i must install the updates, YES or YES", but no, i did the next.

Warning; Before starting doing the 7th step try to install python when you finish the 6th step, if still not working, try starting with the 7th step.

  1. Install vcredist_x86 (if you have 64 bit OS install the 64 bits version).
  2. Go to My PC.
  3. Enter to the local disc (C:/ or whatever letter you have on it).
  4. Second click on Users>Properties>Security>Advanced.
  5. Owner>Edit>In the change owner to list, select new owner>Clic the option. of "Add changes to all subfolders".
  6. Clic ok twice when you finished.
  7. Go to permissions.
  8. Select your user.
  9. Clic full control.
  10. Apply changes and close properties then install python.

This worked for me.

Aitor
  • 88
  • 1
  • 9
0

Funnily enough, it means that access is denied. You don't have permission to kill the process. This could be due to your account level (a "guest" sort of account or an account restricted by group policy) or it could be due to UAC (admin on your own machine but not running as admin—not sure if Windows 7 allows non-elevated process killing, though I would have thought it would).

Aitor
  • 88
  • 1
  • 9
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215