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多种

posted @   一石数字欠我15w!!!  阅读(876)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示