1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#==========================================================================================================================
def run_cmd(command, timeout=10, block=True, shell=False, display=True):
"""
Execute commands in array.
INPUT
command_split: Command to execute in array
timeout: specifies the number of seconds to wait for end of command then returns
block: if True, command is killed after timeout has expired
if false, command runs to completion is a daemonized thread
shell_bool = enable shell
OUTPUT
tupple (Unix output lines, return code)
Output: if block mode, return output line of your command
if handle mode, return handle create by subprocess.Popen
Unix return code: None if command is still running, < 0 if killed by signals, >= 0 for exit codes
"""
command_split = command.split(" ")
try:
if display:
print("\tRun command: %s" %repr(command))
subproc = subprocess.Popen(args=command_split, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, universal_newlines=True)
except OSError, err:
print("\tCommand failed: %s " %str(err))
return "", -1
if not block:
# Run as handle
subproc.poll()
return (str(subproc), subproc.returncode)
else:
# block mode
return run_block_mode(subproc, command, timeout=timeout) |
Partager