Linux 定時重啓 Tomcat、重啓Keepalived

一、在 tomcat 目錄新建一個.sh 文件:java

vi restartTomcat.sh

 

二、輸入內容:apache

# author: Linnuo
# date: 2019-05-31
# Filename: restartTomcat.sh
# Description: 重啓tomcat服務

# 聲明環境變量,可重複使用 
export LANG=zh_CN.UTF-8
export JAVA_HOME=/usr/java/jdk1.8.0_161
export JRE_HOME=${JAVA_HOME}/jre
export TOMCAT_HOME=/tomcat/apache-tomcat-9.0.17/

# 輸出開始時間
echo "[$(date +'%F %T')] >>> Tomcat begin to restart."

# 關閉tomcat服務
${TOMCAT_HOME}/bin/shutdown.sh
 
# Kill all remaining processes 殺掉全部殘留進程,不殺掉會佔用系統資源
pidList=$(ps aux | grep ${TOMCAT_HOME} | grep -v grep | awk '{print $2}')

if [ "${pidList}" != "" ]
then
    echo "殘留的進程號:${pidList}";
    for pid in $pidList; do
        kill -9 $pid
        echo "[$(date +'%F %T')] >>> Kill the process [$pid] successfully."
    done
else
    echo "無 殘留的進程號";
fi

# 啓動tomcat服務
${TOMCAT_HOME}/bin/startup.sh

# 獲取從新啓動後的tomcat服務的進程號
pidList2=$(ps aux | grep ${TOMCAT_HOME} | grep -v grep | awk '{print $2}')
if [ "${pidList2}" != "" ]
then
    echo "啓動後的進程號:${pidList2}";
    echo "[$(date +'%F %T')] >>> Tomcat restart complete."
    echo -e "本次tomcat成功重啓 \n\n"
else
    echo "本次tomcat服務啓動出錯,請查看/logs/catalina.out日誌文件"
fi

# 重啓keepalived服務
systemctl restart keepalived

# 查看狀態
service keepalived status

  JAVA_HOME、TOMCAT_HOMT 根據本身路徑輸入,保存。tomcat

 

三、給.sh 文件賦予執行權限:bash

chmod +x restartTomcat.sh

 

四、運行:spa

./restartTomcat.sh

 

 五、配置定時任務 crond:rest

vi /etc/crontab

 

六、最下面添加紅色內容:日誌

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

25 0 */2 * * root /root/restartTomcat.sh >> /root/restartLog.txt

# 每兩天的 00:25 執行一次 - 用戶 - 執行的腳本路徑 - 執行信息的輸出位置 - 執行信息存放的文件名
 
保存退出後,系統自動讀取加載,間隔到點,會看見 root 目錄下多了個存放執行信息的文件 restartLog.txt
至此,Linux 中 Tomcat\Keepalived 定時重啓的功能完成.
相關文章
相關標籤/搜索