Windows如何设置定时重启Tomcat
项目场景:
系统:Windows 7
Tomcat:apache-tomcat-8.0.5
JDK:1.8
问题描述
最近项目的Tomcat隔一段时间就假死,最后想到的解决方式就是:每天凌晨1点重启tomact。
解决方案:
使用Windows系统的计划任务程序,可以在这里设置定时执行的.bat批处理文件(将你要定时执行的cmd命令放在这里),这样就可以实现让电脑在某个时刻做你想让它干的事。
实现步骤:
一、创建tomcat重启的脚本
创建txt文件restart.txt,编辑内容,把下面的内容复制进去,然后把缀改为.bat,最后文件名为:restart.bat
echo 正在关闭Tomcat服务,请稍等...... @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. rem --------------------------------------------------------------------------- rem Stop script for the CATALINA Server rem --------------------------------------------------------------------------- setlocal rem Guess CATALINA_HOME if not defined set "CURRENT_DIR=%cd%" if not "%CATALINA_HOME%" == "" goto gotHome set "CATALINA_HOME=%CURRENT_DIR%" if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome cd .. set "CATALINA_HOME=%cd%" cd "%CURRENT_DIR%" :gotHome if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" rem Check that target executable exists if exist "%EXECUTABLE%" goto okExec echo Cannot find "%EXECUTABLE%" echo This file is needed to run this program goto end :okExec rem Get remaining unshifted command line arguments and save them in the set CMD_LINE_ARGS= :setArgs if ""%1""=="""" goto doneSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setArgs :doneSetArgs call "%EXECUTABLE%" stop %CMD_LINE_ARGS% :end echo 关闭Tomcat服务完成 ping 127.0.0.1 -n 20 echo ****************************************** echo 正在启动Tomcat服务,请稍等...... @echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor license agreements. See the NOTICE file distributed with rem this work for additional information regarding copyright ownership. rem The ASF licenses this file to You under the Apache License, Version 2.0 rem (the "License"); you may not use this file except in compliance with rem the License. You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. rem --------------------------------------------------------------------------- rem Start script for the CATALINA Server rem --------------------------------------------------------------------------- setlocal rem Guess CATALINA_HOME if not defined set "CURRENT_DIR=%cd%" if not "%CATALINA_HOME%" == "" goto gotHome set "CATALINA_HOME=%CURRENT_DIR%" if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome cd .. set "CATALINA_HOME=%cd%" cd "%CURRENT_DIR%" :gotHome if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" rem Check that target executable exists if exist "%EXECUTABLE%" goto okExec echo Cannot find "%EXECUTABLE%" echo This file is needed to run this program goto end :okExec rem Get remaining unshifted command line arguments and save them in the set CMD_LINE_ARGS= :setArgs if ""%1""=="""" goto doneSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setArgs :doneSetArgs call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end echo 启动Tomcat服务完成
把restart.bat 文件复制到apache-tomcat-8.0.50\bin目录下 ,然后可以直接双击运行,启动会报个错,是因为tomcat没启动,是正常的。

二、创建任务计划程序
1、开始菜单搜索“任务计划程序”

或者到控制面板里面找到“计划任务”

2、弹出任务计划程序界面,在右侧点击创建任务

3、输入 名称和描述

4、点击触发器选项卡,点击左下角新建,弹出新建操作,新建一个触发器,每天凌晨一点触发


5、点击操作选项卡,点击左下角新建,弹出新建操作,点击浏览。选择要执行的脚本restart.bat ,点击打开确定。


附加:
这里说下nginx配置集群和tomcat实现session共享
首先要启动两个tomcat,然后端口不一样,比如8889和8899
1、nginx.conf关键配置
upstream serverlist {
#最少连接数,根据连接数多少自动选择后端服务器,连接数相等时根据权重选择
least_conn;
#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
#ip_hash;
#若120秒内出现2次错误,则下个120秒内不再访问此服务器,weight权重越大,请求机会越多
server 127.0.0.1:8889 max_fails=2 fail_timeout=120s weight=1;#主服务
server 127.0.0.1:8899 max_fails=2 fail_timeout=120s weight=1 backup;#备用tomcat服务,主服务挂了会调用备用服务
}
2、tomcat实现session共享
server.xml在Engine标签内加上配置
修改web项目,工程WEB-INF下的web.xml,添加如下一句配置
到此这篇关于Windows如何设置定时重启Tomcat 的文章就介绍到这了,更多相关Tomcat定时重启内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章
- 12-22nginx代理实现静态资源访问的示例代码
- 12-22Docker 存储管理的几种方式
- 12-22nginx静态资源的服务器配置方法
- 12-22Docker Compose部署微服务项目上线功能
- 12-22GPU服务器的多用户配置方法
- 12-22docker-compose搭建etcd集群的实现(三节点)
- 12-22docker中mysql开启日志的实现步骤
- 12-22Linux下docker安装mysql8并配置远程连接
- 12-22docker部署mysql8并设置可远程连接
- 12-22阿里云oss对象存储使用详细步骤


阅读排行
推荐教程
- 12-11docker存储目录迁移示例教程
- 12-10docker start启动容器后仍然exit状态的解决
- 12-10Linux下如何安装Logstash
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法
- 12-13k8s编排之Deployment知识点详解
- 12-13Nginx IP封禁及自动封禁IP的实现
- 12-13Nginx代理Partainer如何使用





