使用python 自动化监控进程


自动化监控进程在项目中是非常重要的,监控指定程序是否运行,如果程序没有运行,那么启动程序,

下面是使用python来实现的一个进程监控脚本,自己搞的过程中遇到的坑,在这里总结下


下面的我们的python脚本文件




import commands
import os
import sys
import logging

def monitor_process():
  status,output = commands.getstatusoutput("jps -m  |grep 'com.static.app.Main'|wc -l")
 
  #counts = len(schedule)
  #print(counts)
  print(output)
  if (output == '0'):
    os.system("sh /server/statics/start.sh")
    #subprocess.call("sh /server/statics/start.sh",shell=True)   
 
	
#if __name__ == '__main__':
monitor_process()


使用jps -m |grep 'com.static.app.Main'|wc -l   linux命令查询statics进程数



if判断等于0时,启动我的start.sh脚本,start.sh脚本中是启动服务并分配资源的命令,这里不在阐述

之后需要使用linux的定时启动命令在定时执行监控的python脚本

linux中执行    contrab -e



*/5 * * * * source /etc/profile && cd /server/statics/ && python /server/statics/defend.py


这里*/5 * * * *  五分钟启动一次 defend.py


需要注意的是,有时候代码没错,但是程序不执行,需要注意设置一下环境变量才能生效source /etc/profile



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值