Windows Tasklist.exe continuously popup while checking TaskList in subprocess

134 Views Asked by At

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. 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)
1

There are 1 best solutions below

0
Jakub Trllo On

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). To avoid this on windows you can create detached process by passing 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)
    ]