start and kill process by command promt

Discussion in 'Python' started by shanti bhushan, Jun 21, 2010.

  1. 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
     
    shanti bhushan, Jun 21, 2010
    #1
    1. Advertising

  2. On Jun 21, 10:41 am, shanti bhushan <> wrote:
    > 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


    i am getting following error message
    "process cannot acces the file because it is being used by other
    process"
     
    shanti bhushan, Jun 21, 2010
    #2
    1. Advertising

  3. There's no need to use taskill.exe;
    keep a reference of the subprocess.Popen() object around and use its
    kill() method instead.


    --- Giampaolo
    http://code.google.com/p/pyftpdlib
    http://code.google.com/p/psutil


    2010/6/21 shanti bhushan <>:
    > On Jun 21, 10:41 am, shanti bhushan <> wrote:
    >> 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

    >
    > i am getting following error message
    > "process cannot acces the file because it is being used by other
    > process"
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Giampaolo Rodolà, Jun 21, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patio87
    Replies:
    4
    Views:
    435
    Julie
    Aug 18, 2004
  2. shanti bhushan

    subprocess in Command promt+ webbrowser

    shanti bhushan, Jun 19, 2010, in forum: Python
    Replies:
    2
    Views:
    468
    shanti bhushan
    Jun 19, 2010
  3. Shyam Prakash

    Hiding cmd promt window

    Shyam Prakash, Feb 3, 2005, in forum: ASP .Net Security
    Replies:
    2
    Views:
    203
    Shyam Prakash
    Feb 4, 2005
  4. Li Chen
    Replies:
    0
    Views:
    128
    Li Chen
    Nov 26, 2006
  5. priya
    Replies:
    2
    Views:
    133
Loading...

Share This Page