1.

Solve : How to check if a program is running in cmd??

Answer»

So I'm writing some python and I want to make an if statement that checks if a program is RUNNING I figured the simplest way would be to use subprocess and check with cmd if the program is running. Since the program needs to be running at all times, it would also be ideal to have it start the program if it is not running.

I was hoping for SOMETHING such as:

IF [NOT] RUNNING foo.exe foo.exe

but as far as I have found, there isn't really anything like that. Sorry if this in almost unbearably easy, this is my first year really doing any programming, so I'm learning one STEP at a time haha. Code: [Select]tasklist /FI "IMAGENAME eq progname.exe">nul || start progname.exe
tasklist /? for help

tasklist to see a list of running programs.

Quote from: smckee6192 on November 19, 2011, 03:14:25 PM

So I'm writing some python and I want to make an if statement that checks if a program is running I figured the simplest way would be to use subprocess and check with cmd if the program is running.


I found this python code  that uses WMI to get a list of processes on Win32.

Code: [Select]import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Process")
for objItem in colItems:
   PRINT "NAME: ", objItem.Name
   print "File location: ", objItem.ExecutablePath


Discussion

No Comment Found