import psutil
def ifProcessRunning(process_name='WeChat.exe'):
# 判断某个程序是否在运行
# 原理:获取正在运行程序的pid,通过pid获取程序名,再按程序名进行判断
pl = psutil.pids()
result = "PROCESS_IS_NOT_RUNNING"
for pid in pl:
if (psutil.Process(pid).name() == process_name):
if isinstance(pid, int):
result = "PROCESS_IS_RUNNING"
return result
也可以使用下面方式
import psutil
def is_process_running(process_name):
for proc in psutil.process_iter(['name']):
try:
if process_name.lower() in proc.info['name'].lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
# 使用示例
if is_process_running('你的进程名'):
print('进程正在运行')
else:
print('进程未运行')