How to check in Python if Windows process is running when its service has stopped?

Some Windows processes keep running for a few minutes after their service has stopped. Is there a way in Python to detect that?


Asked by: Paul384 | Posted: 30-11-2021






Answer 1

You can try the psutil package and in particular psutil.process_iter() (returns an iterator over running processes). This package is used in other profiler packages. Documentation on Process functions can be found here.

I don't know how you would find process ids for the service(s) in question if they are not apparent in parent/child pid relationships. I've not tested this on Windows.

Answered by: Marcus208 | Posted: 01-01-2022



Answer 2

import os

def getTasks(name):
r = os.popen('tasklist /v').read().strip().split('\n')
print ('# of tasks is %s' % (len(r)))
for i in range(len(r)):
    s = r[i]
    if name in r[i]:
        print ('%s in r[i]' %(name))
        return r[i]
return []

if __name__ == '__main__':
'''
This code checks tasklist, and will print the status of a code
'''

     imgName = 'dwm.exe'

     notResponding = 'Not Responding'

     r = getTasks(imgName)

if not r:
    print('%s - No such process' % (imgName)) 

elif 'Not Responding' in r:
    print('%s is Not responding' % (imgName))

else:
    print('%s is Running or Unknown' % (imgName))

Source

Important note!!! As the author of the tutorial stated the platform he used was Windows Server 2012 but this code most likely work with other Windows products. At the very least it should give you and idea on how to do what you want.

Hope its helps!

Answered by: Roland522 | Posted: 01-01-2022



Similar questions

windows - Using Python, how can I obtain the running process and the exe name for that process?

Suppose that I have an exe running and its name is myexe.exe. On Windows, I can see the process for myexe.exe in Process Explorer. Using Python, how can I obtain the process ID of myexe.exe and shut it down?


windows xp - Process size in XP from Python

I have a python script that can approach the 2 GB process limit under Windows XP. On a machine with 2 GB physical memory, that can pretty much lock up the machine, even if the Python script is running at below normal priority. Is there a way in Python to find out my own process size? Thanks, Gerry


windows - python and process

What is the best way in python find the process name and owner? Now i use WMI, but this version is too slow.


windows - Get process ID with python

How to get the current process id with python on windows? there are this function os.geteuid() but its only works with linux/unix could someone tell what it the pythonic way to get the current process id on windows.


windows - How to list all dlls loaded by a process with Python?

I want to list all the dlls loaded by a process, like this: How could I get the information with Python on Windows?


windows - Using Python, how can I obtain the running process and the exe name for that process?

Suppose that I have an exe running and its name is myexe.exe. On Windows, I can see the process for myexe.exe in Process Explorer. Using Python, how can I obtain the process ID of myexe.exe and shut it down?


windows - Every Python IDLE run starts a new process

Windows 7: I'm using Python3.2 with IDLE. Every time I edit and load my program, I get a new "pythonw.exe *32" process (as shown by Windows Task Manager)--even if the program just prints Hello World. This is a special nuisance if the program is on a static RAM drive, because then I have to kill each of these processes individually before I can eject my drive. Is this a bug in IDLE? Is there a way I can prev...


windows 7 - Keep ram for python process

I have a python script (exe made with py2exe) that runs on several machines. This Python exe does two things periodically; one thread writes a py file for further treatment while another thread listens on a tcp port using the pyZmq module. The has run perfectly for days, using around 12Mo of ram. Now here is the problem : these machines also run 3dsmax rendering. 3dsmax takes ...


winapi - Python Windows API dump process in buffer then REGEX search

I have the following Python code using the Windows APIs to do a minidump of a process, works ok dumping to a file but I need a way to keep the dump in a memory buffer then do a REGEX search against it. Couldn't come out with a way other than dumping to a file. Any idea? import win32security, win32con, win32api, win32file, ctypes import re from constants.structures import MINIDUMP_TYPES_CLASS dbghelp = ctyp...


windows - python 2 [Error 32] The process cannot access the file because it is being used by another process

I'm working with python 2 and have read several posts about this error i.e(this post). However, I'm still getting the error. What I do is: I read the files in a directory, if any of the files contains a specific string, I delete the directory. def select_poo(): path = os.walk('/pail...


windows - Python kill batch file process failed

I'm attempting to use Python subprocess.Popen to run a batch file, which executes xfoil.exe and feeds it some parameters. Then it waits until xfoil.exe exits. I've used the following code as a batch runner, which waits for the process to exit with a timeout. The process is killed if it times out. def Bat_Runner(args, **kwargs): """ Batch runner :par...


windows - Create an icon in memory with win32 in python

What's a good way to generate an icon in-memory in python? Right now I'm forced to use pygame to draw the icon, then I save it to disk as an .ico file, and then I load it from disk as an ICO resource... Something like this: if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, ...


windows xp - Process size in XP from Python

I have a python script that can approach the 2 GB process limit under Windows XP. On a machine with 2 GB physical memory, that can pretty much lock up the machine, even if the Python script is running at below normal priority. Is there a way in Python to find out my own process size? Thanks, Gerry


How to find out if there is data to be read from stdin on Windows in Python?

This code select.select([sys.stdin], [], [], 1.0) does exactly what I want on Linux, but not in Windows. I've used kbhit() in msvcrt before to see if data is available on stdin for reading, but in this case it always returns 0. Additionally msvcrt.getch() returns '\xff' whereas sys.stdin.read(1) returns ...


How can I read system information in Python on Windows?

Following from this OS-agnostic question, specifically this response, similar to data available from the likes of /proc/meminfo on Linux, how can I read system information from Windows using Python (including, but...


In the windows python console, how to make Tab = four spaces?

Hello I would like that when I am in the python console tabbing will give me four spaces. Any ideas?


windows xp - Cygwin and Python 2.6

New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff. Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin components. Will cygwin change or modify any other os functionality or have any other side effects?


windows - Using DPAPI with Python?

Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python? I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow. EDIT: I've taken the example code pointed to by "dF" and tweaked it into a standalone library which can be simply used at a high ...


windows - How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities. I'd like to create a web page served from one of my systems that would merely present a few buttons which would allow me to initiate these scripts remotely. The problem is that I do...


How do I create a windows service with Python


Python script - SCP on windows

How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to switch this off. the remote hosts are running ssh-server on cygwin (win 2003 servers). Thanks






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top