Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to check if a program is running in cmd?  (Read 3900 times)

0 Members and 1 Guest are viewing this topic.

smckee6192

  • Guest
How to check if a program is running in cmd?
« 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. 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.

Salmon Trout

  • Guest
Re: How to check if a program is running in cmd?
« Reply #1 on: November 19, 2011, 05:18:33 PM »
Code: [Select]
tasklist /FI "IMAGENAME eq progname.exe">nul || start progname.exe
tasklist /? for help

tasklist to see a list of running programs.


BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: How to check if a program is running in cmd?
« Reply #2 on: November 20, 2011, 05:59:38 AM »
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
I was trying to dereference Null Pointers before it was cool.