編寫shell腳本實現(xiàn)tomcat定時重啟的方法
最近我在學生價買的低配服務器上部署了一個很吃內存的網頁,導致 tomcat 內存經常溢出而崩潰。
于是我上網找了一些教程編寫了一個簡單的每天定時啟動 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已經啟動,準備使用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
除了這種手動重啟的方法,還有優(yōu)化 tomcat 的方法,這個等有時間再去探索了
到此這篇關于編寫shell腳本實現(xiàn)tomcat定時重啟的方法的文章就介紹到這了,更多相關shell tomcat定時重啟內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
下面小編就為大家?guī)硪黄猻hell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具
這篇文章主要介紹了Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具,本文直接給出實現(xiàn)腳本代碼,需要的朋友可以參考下2015-06-06