1 import subprocess
 2 def exec_command(cmd, log_path, **kwargs):
 3     with open(log_path, 'w') as f:
 4         p = subprocess.Popen(cmd,
 5                              shell=True,
 6                              stdout=f.fileno(),
 7                              stderr=f.fileno(),
 8                              executable='/bin/bash',
 9                              **kwargs)
10     p.communicate() #立即阻塞父进程,直到子进程结束
11     if p.returncode != 0:
12         print('cmd: %s execute failed', cmd)
13     else:
14         print('execute command: %s finished', cmd)
15     return p.returncode

详细介绍subprocess模块:https://www.cnblogs.com/Security-Darren/p/4733368.html

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。