溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用shell腳本怎么定時重啟tomcat

發(fā)布時間:2020-12-11 14:11:16 來源:億速云 閱讀:407 作者:Leah 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關利用shell腳本怎么定時重啟tomcat,文章內(nèi)容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

1、 在某個目錄新建一個 .sh 腳本文件

vim tomcatStart.sh

2、 在 tomcatStart.sh 文件里面寫入一下代碼

#!/bin/bash
/etc/profile
tomcatPath="/usr/local/tomcat9"
binPath="$tomcatPath/bin"
echo "[info][$(date)]正在監(jiān)控tomcat,路徑:$tomcatPath"
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "pid"]; then
echo "[info][$(date)]tomcat進程為:$pid"
echo "[info][$(date)]tomcat已經(jīng)啟動,準備使用shutdown命令關閉"
$binPath"/shutdown.sh"
sleep 2
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "$pid"]; then
echo "[info][$(date)]使用shutdown關閉失敗,準備kill進程"
kill -9 $pid
echo "[info][$(date)]kill進程完畢"
sleep 1
else
echo "[info][$(date)]使用shutdown關閉成功"
fi
else
echo "[info][$(date)]tomcat未啟動"
fi
echo "[info][$(date)]準備啟動tomcat"
$binPath"/startup.sh"

3、 修改 tomcatStart.sh 的權限

sudo chmod 777 tomcatStart.sh

4、 添加腳本到 crontab 定時任務

crontab -e
// 第一個是 tomcatStart.sh 的路徑, 第二個是將日志輸出到某個文件中
00 03 * * * /usr/local/tomcat9/bin/tomcatStart.sh >> /home/zhang/tomcatLog.txt

5、 重啟一下 crontab 以生效

systemctl restart crond

關于利用shell腳本怎么定時重啟tomcat就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI