【python】关闭 os.popen()和subprocess.Popen()方法
cmd = os.popen('adb shell dumpsys activity|findstr mFocus').read()
执行上述代码后会os.popen后台并不会结束
需要改为
cmd = os.popen('adb shell dumpsys activity|findstr mFocus') data = cmd.read() cmd.close()
若是实时抓取log,或者ping之类的命令,建议使用subprocess.Popen
import subprocess,os
from signal import SIGTERM cmd = 'ping www.baidu.com' process = subprocess.Popen(cmd, shell=True,preexec_fn=os.setsid) time.sleep(5) print("停止")
os.killpg(process.pid,SIGTERM)
print("cmd关闭")
或者通过
import os cmd = "adb logcat > 1.txt" kill_cmd = 'ps -ef | grep "adb logcat" | grep -v grep | awk "{print $2}" | xargs kill -9' os.popen(cmd) time.sleep(3) os.popen(kill_cmd)
或者通过adb logcat -d