python3实现windows下同名进程监控
python3实现windows下同名进程监控,供大家参考,具体内容如下
公司老版的SVN服务器的svn服务经常意外关闭,需要写个简单的监控脚本监控一下;
首先多个SVN服务使用不同的端口,使用wmic命令查看所有SVN进程占用的端口以此来判断目标服务是否存活,wimc命令如下:
wmic process where caption=”svn.exe” get commandline /value
然后用正则取出标准输出中的端口,用来比对;
def get_alive_port(program): """ 获取目标程序占用的端口 :param program {string} 目标进程 :return portlist {list} 目标进程占用的端口列表 """ cmd = 'wmic process where caption="%s" get commandline /value' % program ps = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) portlist = [] while True: out = ps.stdout.readline() if out: out = out.decode("gb2312") templist = re.findall("[0-9]{4,5}", out) portlist.extend(templist) else: break return portlist
使用监控后发现SVN服务不意外关闭了,但是SVN程序被访问久了占用过大内存需要监控一下借助psutil来实现;
def howmuch_memory(program): """ 监控目标进程内存是否超过阀值,若超过则关闭 """ cmd = 'wmic process where caption="%s" get processid /value' % program ps = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) pids = [] while True: out = ps.stdout.readline() if out: out = out.decode("gb2312") templist = re.findall("[0-9]{3,6}", out) pids.extend(templist) else: break for pid in pids: try: p = psutil.Process(int(pid)) p_memory = p.memory_info() if int(p_memory.rss / (1024 * 1024)) >= 200: p.kill() except Exception as e: print("出现如下错误:{0}".format(e)) continue
相关推荐
chuckchen 2020-10-31
Will0 2020-10-12
Dreamhome 2020-10-09
xirongxudlut 2020-09-28
星辰大海的路上 2020-09-13
chaochao 2020-08-31
猪猪侠喜欢躲猫猫 2020-08-17
快递小可 2020-08-16
shengge0 2020-07-26
巩庆奎 2020-07-21
张文倩数据库学生 2020-07-19
xirongxudlut 2020-07-18
Ericbig 2020-07-18
kyelu 2020-07-09
liangzhouqu 2020-07-07
GuoSir 2020-06-28
chaigang 2020-06-27
pythonxuexi 2020-06-25