編寫shell腳本實現(xiàn)tomcat定時重啟的方法
最近我在學(xué)生價買的低配服務(wù)器上部署了一個很吃內(nèi)存的網(wǎng)頁,導(dǎo)致 tomcat 內(nèi)存經(jīng)常溢出而崩潰。
于是我上網(wǎng)找了一些教程編寫了一個簡單的每天定時啟動 tomcat 的腳本,特此記錄一下
我的環(huán)境是 centos 7
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)啟動,準(zhǔn)備使用shutdown命令關(guān)閉"
$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關(guān)閉失敗,準(zhǔn)備kill進程"
kill -9 $pid
echo "[info][$(date)]kill進程完畢"
sleep 1
else
echo "[info][$(date)]使用shutdown關(guān)閉成功"
fi
else
echo "[info][$(date)]tomcat未啟動"
fi
echo "[info][$(date)]準(zhǔn)備啟動tomcat"
$binPath"/startup.sh"
3、 修改 tomcatStart.sh 的權(quán)限
sudo chmod 777 tomcatStart.sh
4、 添加腳本到 crontab 定時任務(wù)
crontab -e // 第一個是 tomcatStart.sh 的路徑, 第二個是將日志輸出到某個文件中 00 03 * * * /usr/local/tomcat9/bin/tomcatStart.sh >> /home/zhang/tomcatLog.txt
5、 重啟一下 crontab 以生效
systemctl restart crond
除了這種手動重啟的方法,還有優(yōu)化 tomcat 的方法,這個等有時間再去探索了
到此這篇關(guān)于編寫shell腳本實現(xiàn)tomcat定時重啟的方法的文章就介紹到這了,更多相關(guān)shell tomcat定時重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
下面小編就為大家?guī)硪黄猻hell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具
這篇文章主要介紹了Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具,本文直接給出實現(xiàn)腳本代碼,需要的朋友可以參考下2015-06-06

