Python subprocess执行持续输出shell命令的控制
2016-06-29 15:15
761 查看
研究了大半天,为了获取持续输出的shell指令结果,并对结果进行分析,一直因为无法控制subprocess开启的子进程头疼,研究了半天,参考众多大神的博客后,终于实现,目前已时间为控制点,在实际业务中,可以通过判断业务执行是否完成来达到停止subprocess子进程的目的。
#程序执行终止时间为当前时刻延迟15秒 stoptime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+10)) def run_adbshell(): p = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True) while True: line=p.stdout.readline().strip() print(line) #当前时间 curtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) if curtime>=stoptime: #终止子进程 p.terminate() #等待子进程终止后跳出while循环 if subprocess.Popen.poll(p) is not None: break else: print(u'等待subprocess子进程终止。') print(u'完成')
相关文章推荐
- shell在xcode中涉及到的关键字
- Bash Template
- shell echo 知多少 -n, -e 主要是参数
- batT脚本如何自动执行 adb shell 以后的命令(android抓包)
- 监控磁盘空间shell脚本
- 分享2款XSHELL配色方案及导入配色方案的方法
- shell编程-正则表达式
- SHELL相关
- shell 初识
- PowerShell 使用当前日期创建文件夹
- Linux学习之XShell与虚拟机的连接
- linux shell 实用脚本
- shell小脚本
- (OK)(OK) adb -s emulator-5554 shell - natpf1 - VirtualBox - NAT Port Forwarding
- shell 编程基础
- shell中的条件判断
- shell 获取系统时间
- 使用shell和python分别实现简单菜单功能--打印当前系统状态信息
- XShell 快捷键
- SHELL--待续