Tomcat自启动脚本

import socket
import time
import os
import random

path = r'G:\apache-tomcat-7.0.92-windows-x64\apache-tomcat-7.0.92\bin'  # 服务器tomcat路径
ip = "127.0.0.1"
port = 8080
file_path = r'D:\tomcat\logs'  # 记录每次脚本执行的日志文件文件夹路径


def tomcat(otherStyleTime, logfile):
    os.chdir(path=path)  # 切换到Tomcat工作目录
    os.system("shutdown.bat")  # 首先杀死脚本
    os.system("echo %s   can't connect to port 8080, stop tomcat >> %s" % (otherStyleTime, logfile))
    time.sleep(random.randint(1, 4))  # 随机睡几秒
    os.system("startup.bat")  # 启动脚本
    os.system("echo %s    tomcat restart! >> %s" % (otherStyleTime, logfile))


if __name__ == '__main__':
    while True:
        try:
            # 尝试链接Tomcat
            res = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            res.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            if res.connect((ip, port)):
                time.sleep(4)
                res.shutdown(2)

        except ConnectionRefusedError as e:
            print("ConnectionRefusedError")
            logfile = file_path + '\%s.log' % (time.strftime('%Y%m%d', time.localtime()))  # 日志文件存放处
            now = int(time.time())
            timeArray = time.localtime(now)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 获取固定格式时间的字符串
            tomcat(otherStyleTime, logfile)

        except OSError as e:
            print("OSError")

        finally:
            time.sleep(random.randint(3, 5))

 # 貌似还有更加简单的写法,写法n多种

转载于:https://www.cnblogs.com/52-qq/p/10250530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值