python实现的一对多实时守护脚本

来源:互联网 发布:淘宝p2p 编辑:程序博客网 时间:2024/06/04 20:38

通过配置文件,实现一个守护脚本管理多个不同程序进程

通过修改,删除,增加配置文件实现被守护进程的增删改,且实时生效,不需要重启守护脚本

配置文件一行配置一个进程,包括四个参数,以空格分割,分别为进程的两个关键字,用于唯一确定一条进程,进程启动脚本的目录,启动脚本文件名

重启日志自动生成在启动脚本目录


#!/usr/bin/pythonimport os,time,globconf_dir = '/root/test'conf_path = conf_dir+'/*cfg'keep_sh_path = '/root/keep.sh'interval = 30 def readcfg(pathname):    list = []    filelist = glob.glob(pathname)    for file in filelist:        fo = open(file,'r')        for line in fo.readlines():            list.append(line.strip('\r\n').split())    return listpslist = readcfg(conf_path)while 1:    mtime = os.path.getmtime(conf_dir)    ctime = time.time()    if((mtime + interval) >= ctime):        pslist = readcfg(conf_path)    for psarray in pslist:        os.system('sh ' + keep_sh_path + ' ' + psarray[0] + ' ' + psarray[1] + ' ' + psarray[2] + ' ' + psarray[3] + '>/dev/null 2>&1')    time.sleep(interval)

调用的keep.sh:

#!/bin/shpattern_1=$1pattern_2=$2dir=$3startscript=$4cd $dir || exit 0ps -ef|grep $pattern_1|grep $pattern_2|grep -v grep|grep -v keep.sh >/dev/null 2>&1if [ $? -ne 0 ]; then    sh $startscript && echo "$(date): app($pattern_1 $pattern_2) restarted." >>./restart.logfi


0 0
原创粉丝点击