killing a process in windows

S

Stephen Boulet

This is wierd. I can launch and kill a process in windows, but only if I do
it in the same script.

Just using xemacs as an example, I can launch it like so:

import os, win32api, sys,os.path
prog = "xemacs.exe"
command = os.path.join(base,prog)
mode = os.P_NOWAIT
id = os.spawnl(mode,command)

and then kill it:
win32api.TerminateProcess(id,0)

This works. But I want the script to launch the program if the script is
passed the argument "start", or kill it otherwise. Since there's no way
that I know to query the process ID if python doesn't launch the program in
the first place, I write the ID to a temp file and then read the id to kill
the process. But this doesn't work:

import os, win32api, sys,os.path

argument = sys.argv[1]

base = "C:\\Program Files\\XEmacs\\XEmacs-21.4.13\\i586-pc-win32"
if argument == 'start':
prog = "xemacs.exe"
command = os.path.join(base,prog)

mode = os.P_NOWAIT
id = os.spawnl(mode,command)
f = open(os.path.join(base,'temp.txt'),'wb')
f.write(str(id))
f.close()
else:
id = int(open(os.path.join(base,'temp.txt')).read())
win32api.TerminateProcess(id,0)

Sorry I don't remember the error exactly, but it involved 'improper handle'.

Is there something I'm missing? Is there a way to query for running process
IDs? Thanks.
 
S

Stephen Boulet

The exact error is:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
pywintypes.error: (6, 'TerminateProcess', 'The handle is invalid.')

What seems to be happening is that exiting a python script (leaving a
process running, let's call it "process X", that I launched with the
script) and starting a new script changes the value of the process ID
for "process X".

Stephen said:
This is wierd. I can launch and kill a process in windows, but only if I do
it in the same script.

Just using xemacs as an example, I can launch it like so:

import os, win32api, sys,os.path
prog = "xemacs.exe"
command = os.path.join(base,prog)
mode = os.P_NOWAIT
id = os.spawnl(mode,command)

and then kill it:
win32api.TerminateProcess(id,0)

This works. But I want the script to launch the program if the script is
passed the argument "start", or kill it otherwise. Since there's no way
that I know to query the process ID if python doesn't launch the program in
the first place, I write the ID to a temp file and then read the id to kill
the process. But this doesn't work:

import os, win32api, sys,os.path

argument = sys.argv[1]

base = "C:\\Program Files\\XEmacs\\XEmacs-21.4.13\\i586-pc-win32"
if argument == 'start':
prog = "xemacs.exe"
command = os.path.join(base,prog)

mode = os.P_NOWAIT
id = os.spawnl(mode,command)
f = open(os.path.join(base,'temp.txt'),'wb')
f.write(str(id))
f.close()
else:
id = int(open(os.path.join(base,'temp.txt')).read())
win32api.TerminateProcess(id,0)

Sorry I don't remember the error exactly, but it involved 'improper handle'.

Is there something I'm missing? Is there a way to query for running process
IDs? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top