#-*-Conding: UTF-8 -*-
Import subprocess
Def getpid_windows (process_name ):
"Use cmd_str = tasklist | find/I" xdict.exe "to find the process ID on the Windows platform """
Export _line = 'tasklist | find/I "% s" '% process_name
Pp = subprocess. popen (cmd_line, shell = true, stdout = subprocess. Pipe, stderr = subprocess. Pipe)
Out, err = pp. Communicate ()
If pp. returncode! = 0:
Print 'error: '+ err
Return-1
Elif out. Strip = '': # This is the case where the queried process is not started.
Print 'error: Find process does not srart'
Return-1
Else:
Out_str = out. Strip ()
Print
PID = out_str [30: 34]
Return int (PID)
Def getpid_linux (process_name ):
"Use PS-Ef | grep % S | grep-V grep | awk '{print $2}' to find the process ID on the Linux platform """
Export _line = "PS-Ef | grep % S | grep-V grep | awk '{print $2}'" % process_name
Pp = subprocess. popen (cmd_line, shell = true, stdout = subprocess. Pipe, stderr = subprocess. Pipe)
Out, err = pp. Communicate ()
If pp. returncode! = 0:
Print 'error: '+ err
Return-1
Elif out. Strip () = "":
Print 'error: Find process does not srart'
Return-1
Else:
PID = out. Strip ()
Return int (PID)
If _ name _ = '_ main __':
Name = 'xdict.exe'
PID = getpid_windows (name)
Print PID