为了防止程序死掉,写的一个守护程序

来源:互联网 发布:办公软件培训心得体会 编辑:程序博客网 时间:2024/04/29 12:22
import glob,os,stat,time,sys
def main():
    while True:
        retcs = int(os.popen("ps aux|grep ./chat |wc -l").read())
        log('The System runing..%d.' % retcs)
        if retcs <2:
           os.system("./chat")
           log('reboot...')
           f = file('LOG/CS_LOG.txt','a')
           fs_log = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"/n"
           f.write(fs_log)
           f.close()
        time.sleep(30)      
    return
def log(str):
        print >>sys.stderr, str
if __name__ == "__main__":
    main()



比较简单,就是每过30秒。去看下进程数,如果=1,说明只有grep ./chat 命令在ps里。重新执行下
原创粉丝点击