shell script to monitor and restart Tomcat to send text messages

#!/bin/sh
TomcatID=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}')
StartTomcat=tomcat_startup_path
WebUrl=www.xxx.com  #需测试服务
TomcatMonitorLog=monitor_log_path

Monitor()
{
 if [ $TomcatID ]; then
    TomcatServiceCode=$(curl -I -m 10 -o /dev/null -s -w %{http_code}  $WebUrl)
    if [ $TomcatServiceCode -eq 200 ]; then
        echo "tomcat is running......"
    else
        echo "restart,TomcatID--->>$TomcatID"
        kill -9 $TomcatID 
        echo "kill -9 tomcat ok"
        sleep 3
        echo "sleep 3 ok, starting Tomcat ..."
        $StartTomcat
        echo "send sms ..."
        curl -d "params" "sms_server_url"else
     fi
 
     echo "tomcat process is no exsit, tomcat restart..."
     $StartTomcat
     curl -d "params" "sms_server_url"
 echo "------------tomcat start success-------------" fi } Monitor>>$TomcatMonitorLog

 

Given script execute permissions chmod + x xxx.sh chmod 777 xxx.sh 

Add recurring task

 crontab -e

* / 1 * * * * shell_path executed per minute

Guess you like

Origin www.cnblogs.com/codechange/p/11588257.html