简体   繁体   English

Windows Tasklist.exe 在检查子进程中的 TaskList 时不断弹出

[英]Windows Tasklist.exe continuously popup while checking TaskList in subprocess

I have developed windows application to monitor the current running process and it will scan the running task list via subprocess in defined interval and application running smoothly on IDE but after compiling and execute exe, then it will popup C:\WINDOWS\SYSTEM32\TASKLIST.exe terminal each time when scan the task list.我开发了windows应用程序来监控当前正在运行的进程,它会在定义的时间间隔内通过进程扫描正在运行的任务列表,应用程序在IDE上运行顺利,但编译执行exe后,会弹出C:\WINDOWS\SYSTEM32\TASKLIST。每次扫描任务列表时执行终端。 I am highly appreciate your expert supports to avoid this glitch enter image description here我非常感谢您的专家支持以避免此故障在此处输入图像描述

import subprocess
from time import sleep

def check_process_running(self):
    process_name = b'CUSTOM_APP.exe'
    get_tasklist = 'TASKLIST'
    tasklist = subprocess.check_output(get_tasklist)
    proc_data = [row for row in tasklist.split(b'\n') if row.startswith(process_name)]
    
  
def events_tracker(self):
 
    while True:
        try:
          tasks = self.check_process_running()
          # some calculations 
        except Exception as e:
          logger.exception("event capture triggered exception "+str(e))

      time.sleep(5)

When you have built python application (without console) on windows there are missing sys.stdout and sys.stderr in the process which cause creation of new console (I think it's because of logic in subprocess module).当您在 windows 上构建 python 应用程序(无控制台)时,进程中缺少sys.stdoutsys.stderr ,这会导致创建新控制台(我认为这是因为subprocess进程模块中的逻辑)。 To avoid this on windows you can create detached process by passing creationflags .为了在 windows 上避免这种情况,您可以通过传递creationflags创建分离进程。

This should work:这应该有效:

import subprocess


def check_process_running(process_name):
    tasklist = subprocess.check_output(
        ["TASKLIST.exe"],
        creationflags=(
            subprocess.CREATE_NEW_PROCESS_GROUP
            | subprocess.DETACHED_PROCESS
        )
    )
    return [
        row.strip()
        for row in tasklist.decode().split("\n")
        if row.startswith(process_name)
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM