windows下python subprocess.Popen执行adb 命令进程无法终止

windows下python subprocess.Popen执行adb 命令进程无法终止

logcmd = "adb logcat -v time > C:/log.txt"

self.Popen = subprocess.Popen(logcmd ,stdout = subprocess.PIPE,shell=True)

用上面的方法来获取logcat的信息,它的实际原理是另外开启一个cmd命令来运行adb logcat的命令,即使后面用popen.terminate()也只能关闭cmd的命令,cmd命令被kill掉后,adb的线程由系统来托管,杀死不了adb的进程,从而导致adb logcat不能退出

解决思路:直接启动adb logcat,不能通过cmd命令来启动,就是不能启动cmd进程,然后cmd进程来启动adb进程,代码如下:

 filename = "C:/log.txt"
 logcat_file = open(filename, 'w')
logcmd = "adb logcat -v time"
self.Poplog = subprocess.Popen(logcmd,stdout=logcat_file,stderr=subprocess.PIPE)

最后使用完记得关闭:Poplog.terminate()

————————————————————————————————————————

logcmd = "adb -s devices logcat >logcat.txt"
Poplog = subprocess.Popen(logcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Poplog.wait(6)
(stdoutput,erroutput) = Poplog.communicate()

def Grab_log():
    import os
    import datetime
    dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    dt_ms = datetime.datetime.now().strftime('%Y-%m-%d')
    file = str(file_exsits("D:/Aotoproject/log/" + dt_ms + "_log"))
    print(file)
    s = os.popen("adb devices")
    a = s.read()
    list = a.split('\n')
    deviceList = []
    for temp in list:
        if len(temp.split()) > 1:
            if temp.split()[1] == 'device':
                deviceList.append(temp.split()[0])
    print('本次共扫描出%s个安卓设备' % len(deviceList))
    for devices in deviceList:
        print(devices)
        ###抓取logcat日志###
        logcat_filename = file+"/" + devices +"_"+ dt_ms +"_"+"logcat_log.txt"
        logcat_file = open(logcat_filename, 'w')
        logcmd = "adb -s "+ devices +" logcat "
        Poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)
        sleep(2)
        Poplog.terminate()
        ###抓取Radio日志###
        radio_filename = file+"/"  + devices + "_" + dt_ms + "_" + "radio_log.txt"
        logcat_file = open(radio_filename, 'w')
        logcmd = "adb -s " + devices + " logcat -b radio "
        Poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)
        sleep(2)
        Poplog.terminate()
        ###抓取Main日志###
        main_filename = file+"/"  + devices + "_" + dt_ms + "_" + "main_log.txt"
        logcat_file = open(main_filename, 'w')
        logcmd = "adb -s " + devices + " logcat -b main "
        Poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)
        sleep(2)
        Poplog.terminate()
        ###抓取Event日志###
        event_filename = file+"/"   + devices + "_" + dt_ms + "_" + "event_log.txt"
        logcat_file = open(event_filename, 'w')
        logcmd = "adb -s " + devices + " logcat -b event "
        Poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)
        sleep(2)
        Poplog.terminate()
        ###抓取kernel日志###
        kernel_filename = file+"/"  + devices + "_" + dt_ms + "_" + "kernel_log.txt"
        logcat_file = open(kernel_filename, 'w')
        logcmd = "adb -s " + devices + " cat /proc/kmsg"
        Poplog = subprocess.Popen(logcmd, stdout=logcat_file, stderr=subprocess.PIPE)
        sleep(2)
        Poplog.terminate()
        print(devices,"抓取完成")

原文链接:

http://www.th7.cn/Program/Python/201507/513606.shtml

猜你喜欢

转载自blog.csdn.net/weixin_40895135/article/details/115166039