@检查Windows上EXE进程是否存在/运行
检查Windows上EXE进程是否存在/运行
最近要做个服务器进程状态的检查脚本,Linux直接用shell搞定比较方便。举例如:
ps aux | grep *** | grep -v grep > /dev/null
if [ $? -eq 0 ];then
echo "*** is running."
else
echo "*** is not running."
fi
本文以windows为例,语言选用python(不上头,容易上手),这里使用了两种方法。
- 安装好依赖
python -m pip install pypiwin32
(方法2) - 不多啰嗦,直接上代码
#!C:/python36/python
# -*- coding: utf-8 -*-
# @Time : 2020/7/8 18:58
# @Author : itzk
# -*- coding: utf-8 -*-
import os
from win32com.client import GetObject
def get_process(process_name):
match = os.popen('tasklist /FI "IMAGENAME eq %s"' % process_name)
process_num = match.read().count(process_name)
return True if process_num > 0 else False
def get_process2(process_name):
is_exist = False
wmi = GetObject('winmgmts:')
processCodeCov = wmi.ExecQuery('select * from Win32_Process where name=\"%s\"' % (process_name))
if len(processCodeCov) > 0:
is_exist = True
return is_exist
if __name__ == "__main__":
res = get_process("chrome.exe") # 方法1
# res = get_process2("chrome.exe") # 方法2
msg = "chrome.exe is running." if res else "chrome.exe is not running."
print(msg)
感谢阅读,欢迎多多批评指正!