Windows如何設(shè)置定時(shí)重啟Tomcat
項(xiàng)目場(chǎng)景:
系統(tǒng):Windows 7
Tomcat:apache-tomcat-8.0.5
JDK:1.8
問題描述
最近項(xiàng)目的Tomcat隔一段時(shí)間就假死,最后想到的解決方式就是:每天凌晨1點(diǎn)重啟tomact。
解決方案:
使用Windows系統(tǒng)的計(jì)劃任務(wù)程序,可以在這里設(shè)置定時(shí)執(zhí)行的.bat批處理文件(將你要定時(shí)執(zhí)行的cmd命令放在這里),這樣就可以實(shí)現(xiàn)讓電腦在某個(gè)時(shí)刻做你想讓它干的事。
實(shí)現(xiàn)步驟:
一、創(chuàng)建tomcat重啟的腳本
創(chuàng)建txt文件restart.txt,編輯內(nèi)容,把下面的內(nèi)容復(fù)制進(jìn)去,然后把綴改為.bat,最后文件名為:restart.bat
echo 正在關(guān)閉Tomcat服務(wù),請(qǐng)稍等...... @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 關(guān)閉Tomcat服務(wù)完成 ping 127.0.0.1 -n 20 echo ****************************************** echo 正在啟動(dòng)Tomcat服務(wù),請(qǐng)稍等...... @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 啟動(dòng)Tomcat服務(wù)完成
把restart.bat 文件復(fù)制到apache-tomcat-8.0.50\bin目錄下 ,然后可以直接雙擊運(yùn)行,啟動(dòng)會(huì)報(bào)個(gè)錯(cuò),是因?yàn)閠omcat沒啟動(dòng),是正常的。

二、創(chuàng)建任務(wù)計(jì)劃程序
1、開始菜單搜索“任務(wù)計(jì)劃程序”

或者到控制面板里面找到“計(jì)劃任務(wù)”

2、彈出任務(wù)計(jì)劃程序界面,在右側(cè)點(diǎn)擊創(chuàng)建任務(wù)

3、輸入 名稱和描述

4、點(diǎn)擊觸發(fā)器選項(xiàng)卡,點(diǎn)擊左下角新建,彈出新建操作,新建一個(gè)觸發(fā)器,每天凌晨一點(diǎn)觸發(fā)


5、點(diǎn)擊操作選項(xiàng)卡,點(diǎn)擊左下角新建,彈出新建操作,點(diǎn)擊瀏覽。選擇要執(zhí)行的腳本restart.bat ,點(diǎn)擊打開確定。


附加:
這里說(shuō)下nginx配置集群和tomcat實(shí)現(xiàn)session共享
首先要啟動(dòng)兩個(gè)tomcat,然后端口不一樣,比如8889和8899
1、nginx.conf關(guān)鍵配置
upstream serverlist {
#最少連接數(shù),根據(jù)連接數(shù)多少自動(dòng)選擇后端服務(wù)器,連接數(shù)相等時(shí)根據(jù)權(quán)重選擇
least_conn;
#每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session的問題
#ip_hash;
#若120秒內(nèi)出現(xiàn)2次錯(cuò)誤,則下個(gè)120秒內(nèi)不再訪問此服務(wù)器,weight權(quán)重越大,請(qǐng)求機(jī)會(huì)越多
server 127.0.0.1:8889 max_fails=2 fail_timeout=120s weight=1;#主服務(wù)
server 127.0.0.1:8899 max_fails=2 fail_timeout=120s weight=1 backup;#備用tomcat服務(wù),主服務(wù)掛了會(huì)調(diào)用備用服務(wù)
}2、tomcat實(shí)現(xiàn)session共享
server.xml在Engine標(biāo)簽內(nèi)加上配置
<Engine name="Catalina" defaultHost="localhost">
<!-- tomcat集群session共享 -->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
</Engine>修改web項(xiàng)目,工程WEB-INF下的web.xml,添加如下一句配置
<distributable />
到此這篇關(guān)于Windows如何設(shè)置定時(shí)重啟Tomcat 的文章就介紹到這了,更多相關(guān)Tomcat定時(shí)重啟內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux部署Tomcat發(fā)布項(xiàng)目過(guò)程中各種問題及解決方法
這篇文章主要介紹了解決Linux部署Tomcat發(fā)布項(xiàng)目過(guò)程中各種問題,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Windows 下修改Tomcat jvm參數(shù)的方法
這篇文章主要介紹了Windows 下修改Tomcat jvm參數(shù)的相關(guān)資料,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
使用Maven tomcat:run命令啟動(dòng)web項(xiàng)目時(shí)修改默認(rèn)端口的方法
今天小編就為大家分享一篇使用Maven tomcat:run命令啟動(dòng)web項(xiàng)目時(shí)修改默認(rèn)端口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
詳述 tomcat 中的 server.xml 配置文件示例
本文將通過(guò)實(shí)例,介紹server.xml中各個(gè)組件的配置,并詳細(xì)說(shuō)明 Tomcat 各個(gè)核心組件的作用以及各個(gè)組件之間的相互關(guān)系。對(duì)tomcat server.xml 配置文件相關(guān)知識(shí)感興趣的朋友一起看看吧2017-08-08
Tomcat宕機(jī)自動(dòng)重啟和每日定時(shí)啟動(dòng)的實(shí)現(xiàn)方法
在Web應(yīng)用開發(fā)中,Tomcat作為一款輕量級(jí)的Web服務(wù)器,被廣泛應(yīng)用于Java Web應(yīng)用的部署,然而,在實(shí)際運(yùn)行過(guò)程中,Tomcat可能會(huì)因?yàn)楦鞣N原因而意外宕機(jī),實(shí)現(xiàn)Tomcat的宕機(jī)自動(dòng)重啟和每日定時(shí)啟動(dòng)是非常必要的,本文將介紹如何通過(guò)簡(jiǎn)單的腳本配置來(lái)實(shí)現(xiàn)這一目標(biāo)2025-02-02
Linux下定時(shí)切割Tomcat日志并刪除指定天數(shù)前的日志記錄
這篇文章主要介紹了Linux下定時(shí)切割Tomcat日志并刪除指定天數(shù)前的日志記錄,需要的朋友可以參考下2017-08-08
IntelliJ?IDEA中配置Tomcat超詳細(xì)教程
這篇文章主要介紹了IntelliJ?IDEA中配置Tomcat超詳細(xì)教程,需要的朋友可以參考下2022-08-08

