Subscribe:

Ads 468x60px

Wednesday, November 21, 2012

How to open and close a process from a python script

Hi guys

     I am giving some idea about  how we can start and stop a process using a python script. This will be useful when we need to start a process that is outside the script

I am running two python scripts. One is master script, this script will start the slave script. Slave script will execute continuously upto 5 sec and it will terminated by the master script

Master script

 #!/usr/bin/python  
 import time  
 import subprocess  
 #starting process python while.py  
 proc = subprocess.Popen(["python","while.py"],shell=False)  
 #5 sec delay  
 time.sleep(5)  
 #killing process  
 subprocess.call(["kill","-9","%d" % proc.pid])  
 #waiting for killing  
 proc.wait()  
 print 'poll = ',proc.poll()  


Slave script


 #!/usr/bin/python  
 while 1:  
      print "hello"  


Output
--------

It will print hello upto 5 seconds after that it will stop and show this message






0 comments:

Post a Comment