code monitor.py
#!/usr/bin/env python
# coding:utf-8
import os
import logging
#日志格式
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(filename='monitor.log', level=logging.DEBUG, format=LOG_FORMAT)
process = '/home/work/monitor/run.lock'
#需要监控的服务
servers = ['bill_data_cur/','bill_data_cur_new']
#服务脚本目录
path = '/home/proxy/bin/'
for server in servers:
os.system('jps -m|grep %s > %s'%(server,process))
if not (os.path.getsize(process)):
os.system("sh %s%s.sh"%(path,server.replace("/","")))
logging.info("checking...")
通过执行shell jps -m|grep 输出文本的形式确定程序是否运行
如果未运行将程序启动
crontab -e 调度
0 * * * * python /home/work/monitor/monitor.py
通过crontab每小时调度一次
cron 日志查看地址: /var/log/cron
网友评论