473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trouble killing a process on windows

Hi, I'd like to start a program, run it for a while, then terminate
it. I can do this on linux, but I'm new to working with windows.
Here's my script:

from subprocess import Popen
from time import sleep
import win32api
war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
sleep(30)
print "slept for 30"
print win32api.TerminateProcess(int(war3game._handle),-1)
#print
ctypes.windll.kernel32.TerminateProcess(int(war3ga me._handle),-1)
print "terminated process"

Here's the output:
slept for 30
Traceback (most recent call last):
File "C:\Python24\warcraft\runwar3.py", line 7, in ?
print win32api.TerminateProcess(int(war3game._handle),-1)
error: (5, 'TerminateProcess', 'Access is denied.')

I'm logged in as adminstrator. Does anyone know how to fix this
problem?
Thanks for your time,
Tom

Jun 2 '07 #1
4 4583
Thomas Nelson wrote:
from subprocess import Popen
from time import sleep
import win32api
war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
sleep(30)
print "slept for 30"
print win32api.TerminateProcess(int(war3game._handle),-1)
#print
ctypes.windll.kernel32.TerminateProcess(int(war3ga me._handle),-1)
print "terminated process"

Here's the output:
slept for 30
Traceback (most recent call last):
File "C:\Python24\warcraft\runwar3.py", line 7, in ?
print win32api.TerminateProcess(int(war3game._handle),-1)
error: (5, 'TerminateProcess', 'Access is denied.')

I'm logged in as adminstrator. Does anyone know how to fix this
problem?
There's nothing obvious. I assume you got your info
from here?

http://aspn.activestate.com/ASPN/Coo.../Recipe/347462

Just for completeness, have you tried the pid-based technique
shown there? I've no idea why it should work if this one
doesn't but... (I have a slight suspicion that the fact that
it opens the process with a "for-termination" flag might help).

Failing that, you can see if WMI can do it (although I assume
that, under the covers, WMI just calls TerminateProcess):

http://timgolden.me.uk/python/wmi_co...estroy_notepad

I suppose you might have to adjust your token privs to include,
say the Debug priv. This is designed to let you take control
of any process (and terminate it, or whatever). If it looks
like you need to do that, post back and I -- or someone else --
can try to run up an example.

TJG
Jun 2 '07 #2
On Jun 2, 12:27 pm, Thomas Nelson <t...@mail.utexas.eduwrote:
Hi, I'd like to start a program, run it for a while, then terminate
it. I can do this on linux, but I'm new to working with windows.
Here's my script:

from subprocess import Popen
from time import sleep
import win32api
war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
sleep(30)
print "slept for 30"
print win32api.TerminateProcess(int(war3game._handle),-1)
#print
ctypes.windll.kernel32.TerminateProcess(int(war3ga me._handle),-1)
print "terminated process"

Here's the output:
slept for 30
Traceback (most recent call last):
File "C:\Python24\warcraft\runwar3.py", line 7, in ?
print win32api.TerminateProcess(int(war3game._handle),-1)
error: (5, 'TerminateProcess', 'Access is denied.')

I'm logged in as adminstrator. Does anyone know how to fix this
problem?
Thanks for your time,
Tom
kill = Popen(['taskkill', war3game.pid])

*shrugs*

maybe it is just taskill, or maybe you need the full path.
don't recall as I quit using windows.

Takskill on XP and newer maybe

Jun 2 '07 #3
On Jun 2, 11:43 am, Tim Golden <m...@timgolden.me.ukwrote:
Thomas Nelson wrote:
from subprocess import Popen
from time import sleep
import win32api
war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
sleep(30)
print "slept for 30"
print win32api.TerminateProcess(int(war3game._handle),-1)
#print
ctypes.windll.kernel32.TerminateProcess(int(war3ga me._handle),-1)
print "terminated process"
Here's the output:
slept for 30
Traceback (most recent call last):
File "C:\Python24\warcraft\runwar3.py", line 7, in ?
print win32api.TerminateProcess(int(war3game._handle),-1)
error: (5, 'TerminateProcess', 'Access is denied.')
I'm logged in as adminstrator. Does anyone know how to fix this
problem?

There's nothing obvious. I assume you got your info
from here?

http://aspn.activestate.com/ASPN/Coo.../Recipe/347462

Just for completeness, have you tried the pid-based technique
shown there? I've no idea why it should work if this one
doesn't but... (I have a slight suspicion that the fact that
it opens the process with a "for-termination" flag might help).

Failing that, you can see if WMI can do it (although I assume
that, under the covers, WMI just calls TerminateProcess):

http://timgolden.me.uk/python/wmi_co...estroy_notepad

I suppose you might have to adjust your token privs to include,
say the Debug priv. This is designed to let you take control
of any process (and terminate it, or whatever). If it looks
like you need to do that, post back and I -- or someone else --
can try to run up an example.

TJG
I Tried the PID method, and the Taskkill method, and neither worked.
This is what I get for trying to work on a windows machine. If you
could explain how to add the debug privilege, that would be great.
Just out of curiosity, is there a reason a python module like os
couldn't have a universal terminateProcess type function, that works
on all systems? Something like os.listdir seems to work really well
everywhere. Maybe killing processes is too complicated for this?

Thanks for the help,
Tom

Jun 3 '07 #4
Thomas Nelson wrote:

[... re Access Denied error on trying to TerminateProcess ...]

[Tim Golden]
>I suppose you might have to adjust your token privs to include,
say the Debug priv. This is designed to let you take control
of any process (and terminate it, or whatever). If it looks
like you need to do that, post back and I -- or someone else --
can try to run up an example.
[Thomas Nelson]
I Tried the PID method, and the Taskkill method, and neither worked.
This is what I get for trying to work on a windows machine. If you
could explain how to add the debug privilege, that would be great.
OK, this being Windows security, there's a couple
of hoops to jump through, but nothing too bad.

<code>
import win32api
import win32security as sec

#
# This function is merely window-dressing
# to make it easier to see whether the
# code has worked or not.
#
def priv_status (hToken, priv):
for p, status in sec.GetTokenInformation (
hToken,
sec.TokenPrivileges
):
if p == priv:
if status == 0:
return "disabled"
else:
return "enabled"
else:
return "disabled"

process = win32api.GetCurrentProcess ()
token_access_flags = sec.TOKEN_ADJUST_PRIVILEGES | sec.TOKEN_QUERY
hToken = sec.OpenProcessToken (process, token_access_flags)
debug_priv = sec.LookupPrivilegeValue (None, sec.SE_DEBUG_NAME)

print "Debug is", priv_status (hToken, debug_priv)

privs = [(debug_priv, sec.SE_PRIVILEGE_ENABLED)]
sec.AdjustTokenPrivileges (hToken, 0, privs)

print "Debug is", priv_status (hToken, debug_priv)

</code>
Just out of curiosity, is there a reason a python module like os
couldn't have a universal terminateProcess type function, that works
on all systems? Something like os.listdir seems to work really well
everywhere. Maybe killing processes is too complicated for this?
Not to say that there could never be one, but I suspect
that the difference of APis and the number of corner
cases makes it a daunting task for maintenance. The
code would have to work on every platform Python works
on -- which is no small number -- and would have to be
maintained across the many and varied changes on each
of those platforms.

Still, if you think you're in with a chance, go ahead
and offer :)

Let us know if the DEBUG priv thing works or not.

TJG
Jun 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John E. Hadstate | last post by:
Please forgive the heavy cross-posting and feel free to delete inappropriate groups when you reply. I am really at a loss and hope someone has an answer for my problems. Environment: Redhat...
35
by: Michael Kearns | last post by:
I've been using python to write a simple 'launcher' for one of our Java applications for quite a while now. I recently updated it to use python 2.4, and all seemed well. Today, one of my...
8
by: Harlin Seritt | last post by:
I am using os.getpid() to get the pid value for a script running. I store that value (as a string) to a file. Later when I try to kill that pid (i pull this from the file as a string value) I get ...
3
by: clsmith66 | last post by:
I am building an ASP.NET application where I have been required to make all the editing screens popup windows within the application. I didn't have any trouble creating the new windows but only...
2
by: Nisha | last post by:
Hello everyone, I have a web page which needs to open files of type .pdf (adobe) and ..doc(microsoft word). I tried something fairly simple and have been having a lot of trouble getting it to...
2
by: Nisha | last post by:
Hello everyone, I have a web page which needs to open files of type .pdf (adobe) and ..doc(microsoft word). I tried something fairly simple and have been having a lot of trouble getting it to...
3
by: Rico | last post by:
Hello, I have a windows service that I have created in VB 2005. I have it installed, and running, but it's not functioning correctly. When I try to "Attach to process", my service is greyed...
2
by: Matt F | last post by:
I have a relatively long command that I'm attempting to get process.start to execute. When executing the command, I get a "The system cannot find the file specified" error. I've gone to the...
3
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'd like to know how to access another process' main window (if it exists) and get information like screen location and size, and whether it's visible at the moment. Is something like that possible?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.