0

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
    Show the relevant code properly formatted in the question. If possible, create an [MRE](https://stackoverflow.com/help/minimal-reproducible-example) – Michael Butscher Jan 04 '23 at 09:18

1 Answers1

0

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)
    ]