Feb 20, 2014

python check process running

We make use of python subprocess module
  • To write/execute the command in the command line
  • To write to stdin and read the output of stdout

Methods Used
1) checkProcessRunning
        To check whether process in running or not

2) writeToCMD
        To execute the command in the command line and read the output of it

Pass the required arguments
checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")

cmd -> Command to execute
processName -> Text to search from the command line output

How to Run
python python_check_process_running.py
import subprocess

def checkProcessRunning(cmd, processName):
    print "\n Process to check : " + cmd
    result = writeToCMD(cmd)
    if (result[0].rstrip().find(processName) != -1):
        print processName + " is present/running"
        return True
    else:
        print processName + " is not present/running"
        return False
          
def writeToCMD(cmd):
    proc = None       
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell="True")
    stdout_value = proc.communicate()
    if stdout_value:
        return stdout_value

checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")
  


No comments:

Post a Comment