473,402 Members | 2,050 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,402 software developers and data experts.

killing a process in windows

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.

--
Stephen
From here to there
and there to here,
funny things are everywhere. -- Dr Seuss

Jul 18 '05 #1
1 7917
The exact error is:
win32api.TerminateProcess(100,0)
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 Boulet wrote: 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.


Jul 18 '05 #2

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

Similar topics

10
by: Jacek Popławski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
0
by: net guest via DotNetMonster.com | last post by:
Hi, i am facing a lot of problems in killing the orphan excel instances. I approached one method like killing the Excel Orphan with processID. I am getting all the Processess that are running...
0
by: Shruti A via .NET 247 | last post by:
hello group I have recently started working on .Net platform. I am facing one problem in killing and starting process from my aspx page. I am able to kill and start the same process from vb.net...
3
by: kunal.kewalramani | last post by:
I'm opening an Excel file using ASP.NET, but Excel process remains it is not killed, I tried killing it by using Quit() that is also not working, if anybody have any solution for this please help me...
10
by: shiry | last post by:
Hi, I need to do some important cleanup before my console application exists. I used the console ctrl event. This is working well and it fires for all cases, including the CTRL_CLOSE_EVENT (if I...
8
by: Rain | last post by:
Does anyone know how i can kill a process using C#? im developing an asp.net web application using C# and the asp.net process doesnt die even if the application has already exited. So i need to...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
4
by: Miro | last post by:
I'm using VB.Net 2003 I have code - it works great - to kill a process(s) if they are running. ---- Dim myProcesses() As Process 'Funny - withouth the () in the myProcesses it does not work...
4
by: Thomas Nelson | last post by:
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.