def execute(cmd, shell=False, root=False, timeout=10):
try:
if root:
cmd.insert(0, 'sudo')
LOG.info(cmd)
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=shell)
timer = Timer(timeout, lambda proc: proc.kill(), [subproc])
timer.start()
subproc.wait()
stdcode = subproc.returncode
stdout = subproc.stdout.readlines()
stderr = subproc.stderr.readlines()
timer.cancel()
def list_strip(lines):
return [line.strip() for line in lines]
return stdcode, list_strip(stderr) if stdcode else list_strip(stdout)
except Exception as e:
LOG.error(e)
raise
评论列表
文章目录