imp
imp

Reputation: 2087

How to know whether a process in windows is running or not in python

I am using python 2.7 and windows 7 64 bit. I want to know whether a process(python.exe) is running or not in task manager/Processes. I had gone through http://www.videntity.com/2010/05/check-to-make-sure-a-process-is-running-and-restart-it-if-its-not-a-recipe-in-python/, but it is not for windows.

Upvotes: 1

Views: 3987

Answers (3)

OrangeCube
OrangeCube

Reputation: 398

The page you linked uses os.popen()(official docs here)

In windows, you should use "tasklist" as arg for os.popen(), rather than "ps -Af"

e.g.

>>> import os
>>> tmp = os.popen("tasklist").read()  # it would return a str type
>>> "python.exe" in tmp
True

Upvotes: 3

wnnmaw
wnnmaw

Reputation: 5514

Here's how I do it with win32:

from win32com.client import GetObject
WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')

if "python.exe" in [process.Properties_('Name').Value for process in processes]:
    #do the thing

Upvotes: 3

Alex Koukoulas
Alex Koukoulas

Reputation: 996

You should be able to see your process in the Background Processes in the processes tab of the task manager with the name pythonw.exe(64 bit)

Upvotes: 0

Related Questions