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定时重启内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章
- 02-02hadoop动态增加和删除节点方法介绍
- 02-02干货 | Linux新手入门好书推荐
- 02-02linux系统下MongoDB单节点安装教程
- 02-02Linux下nginx生成日志自动切割的实现方法
- 02-02Centos 6中编译配置httpd2.4的多种方法详解
- 02-02CentOS7 下安装telnet服务的实现方法
- 02-02分布式Hibernate search详解
- 02-02Hadoop对文本文件的快速全局排序实现方法及分析
- 02-02CentOS6.3添加nginx系统服务的实例详解
- 02-02Hadoop编程基于MR程序实现倒排索引示例


阅读排行
推荐教程
- 12-07一文教你怎么选择Tomcat对应的JDK版本
- 12-07解决tomcat启动报错:一个或多个listeners启动失败问题
- 12-07Tomcat启动报错:严重: Unable to process Jar entry [m
- 12-07Tomcat配置IPV6的实现步骤
- 12-07tomcat启动报错jar not loaded的问题
- 12-11docker存储目录迁移示例教程
- 01-07windows server 2008安装配置DNS服务器
- 12-07Tomcat部署war包并成功访问网页详细图文教程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程




