问题描述
subprocess.Popen执行adb shell指令进入linux系统后出现cmd窗口阻塞问题,需要手动关闭cmd才会继续执行其他指令。
解决方案
1、cmd指令后面加入exit\n
关闭exe进程
2、subprocess.Popen()添加内置参数creationflags=subprocess.CREATE_NO_WINDOW
隐藏窗口弹出
参考代码
import subprocess
import os
def command(cmd):
try:
process =subprocess.Popen(["adb","shell"], shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, creationflags=subprocess.CREATE_NO_WINDOW)
outs,error= process.communicate((cmd+'exit\n').encode())
content = [z.strip() for z in outs.decode("gbk").split("\n") if z]
print(*content,sep="\n")
except Exception as e:
process.kill()
print(e, e.__traceback__.tb_lineno, '行')
command("grep 'app_id = 17' mnt/log/rmu.log\n")