start and kill process by command promt

shanti bhushan ershantibhushan at gmail.com
Mon Jun 21 01:41:07 EDT 2010


Hi,
I want to invoke local wen server named mogoose, and kill it after
some time by python script.
Then i want to change the argument and invoke it again.

I am able to do this with below code.

import subprocess
import time

def invoke_server1():
    s1ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe >YourOutput.txt"')
    print s1ret

def invoke_server2():
    s2ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New1\ >YourOutput.txt"')
    print s2ret
def invoke_server3():
    s3ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New2\ >YourOutput.txt"')
    print s3ret

def kill_server():
    killret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"')
    print killret

kill_server()
time.sleep(5)
invoke_server1()
time.sleep(5)
kill_server()
time.sleep(5)
invoke_server2()
time.sleep(5)
kill_server()
time.sleep(10)
invoke_server3()
time.sleep(5)
kill_server()



This works fine in python26 ,but when i use this code in one testing
tool, server is invoked 1st time properly.but for second time it says
process already occupied cannot access the process. I am not able to
understand why subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
"taskkill /F /IM mongoose-2.8.exe  is not working second time .Please
guide me in this respect.

Or please tell me is there any other way,to do same thing with python
script that will start and kill windows process for my requirement.

Regards
Bhushan



More information about the Python-list mailing list