加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

TOMCAT意外停止后自启动服务器监测

发布时间:2020-12-17 17:28:52 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #-*-coding:utf-8-*-#python3.3.5import urllib.request,os,time,configparser#============================================== class AutoStar: def

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#-*-coding:utf-8-*-
#python3.3.5
import urllib.request,os,time,configparser
#==============================================     
class AutoStar:
    def connet(self,url,tpath):
        status = 404
        try:
            status = urllib.request.urlopen(url).code
        except Exception as err:
            print ('request Exception 404')
        if status > 299 :
            self.error ('http_status. ' + str(status))
            os.chdir(tpath)
            self.error("shutdown. " + tpath)
            os.system('shutdown.bat')
            time.sleep(10)
            self.error("startup. " + tpath)
            os.system('startup.bat')
        else:
            self.debug ('http_status. ' + str(status))
        os.chdir(pyPath)
        self.debug ("-"*20) 
             
    def debug(self,msg):
        print(msg) 
        self.mkLogDir()
        output = open(pyPath + '/logs/debug.log','a')
        output .write("n" + strTime + ' ' + msg)
        output .close()
    def error(self,msg): 
        print(msg)
        self.mkLogDir()
        output = open(pyPath + '/logs/error.log','a')
        output .write("n" + strTime + ' ' + msg)
        output .close()
    def mkLogDir(self): 
        if os.path.isdir(pyPath +'/logs'): 
            pass
        else: 
            os.mkdir(pyPath +'/logs') 
#============================================== 
aStar = AutoStar()
print(">"*50,"start")
conf = 'conf'
pyPath = os.getcwd()
strTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
while 1:
    for dirpath,dirnames,filenames in os.walk(conf):
        #print ('Directory',dirpath)
        for filename in filenames:  
            aStar.debug ('read config file. ' + filename)
            config=configparser.ConfigParser() 
            config.read(conf + '/' +filename)  
            url = config.get("global","url")
            tpath = config.get("global","tomcat_path")
            aStar.connet(url,tpath)
    time.sleep(60)
    aStar.debug("="*30 + ">")

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读