tomcat管理脚本

 1 #!/bin/bash
  2 # #########################################################
  3 ###########################################################
  4 # chkconfig: 2345 96 14 ###################################
  5 # #########################################################
  6
  7 JDK_HOME=/opt/apps/jdk
  8 CATALINA_HOME=/opt/apps/tomcat
  9 export JDK_HOME CATALINA_HOME
 10 source /etc/profile
 11 #PID=`ps -ef  | grep  -v grep  | grep java | awk  '{print $2}'`
 12 #NUM=`ps -ef  | grep  -v grep  | grep java | awk  '{print $2}' | wc -l`
 13
 14 #case $1 in
 15 start() {
 16         echo "正在判断服务状态,请稍等!"
 17         echo "请稍等3秒钟"
 18         echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
 19         if      netstat -na | grep 8080 | grep LISTEN >/dev/null
 20         then
 21                 echo "Tomcat已经正在运行了!"
 22         else
 23                 echo "Tomcat没有运行,1秒后启动!"
 24                 echo 1;sleep 1
 25                 $CATALINA_HOME/bin/catalina.sh start
 26                 echo  "Tomcat 已经成功启动完成,5秒后判断是否启动成功"
 27                 echo "5";sleep 1;echo "4";sleep 1
 28         echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
 29         if  netstat -an | grep 8080 | grep LISTEN >/dev/null
 30             then
 31                 PID=`ps -ef | grep  tomcat | grep jdk | awk '{print $2}'`
 32                 NUM=`ps -ef | grep  tomcat | grep jdk | awk '{print $2}' | wc -l`
 33                 echo "Tomcat 已经成功启动${NUM} 个Tomcat进程!,PID为${PID}"
 34             else
 35                 echo "Tomcat启动失败,请重新启动!"
 36                 echo 1
 37         fi
 38         fi
 39         }
 40 stop() {
 41                 PID=`ps -ef  | grep  -v grep  | grep java | awk  '{print $2}'`
 42                 NUM=`ps -ef | grep  -v "color"  | grep tomcat | awk '{print $2}' | wc -l`
 43                 echo "正在判断服务状态,请稍等3秒钟!"
 44                 echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
 45         if  netstat -an | grep 8080 | grep LISTEN >/dev/null
 46            then
 47                 echo "Tomcat运行中,1秒后关闭!"
 48                 echo  1;sleep 1
 49                 echo "即将关闭Tomcat服务,请稍等!"
 50         $CATALINA_HOME/bin/catalina.sh stop ;echo "已经执行关闭命令,正在检查关闭了多少Tomcat进程,请稍等3秒钟!"
 51                 sleep 3
 52         echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
 53                 # 此命令会关闭主机内所有java进程,慎用!!
 54                 pkill java && pkill tomcat
 55                 if  netstat -an | grep 8080 | grep LISTEN >/dev/null;then
 56                         PID=`ps -ef  | grep  -v grep  | grep java | awk  '{print $2}'`
 57                         NUM=`ps -ef | grep  -v "color"  | grep tomcat | awk '{print $2}' | wc -l`
 58                         kill -9 $PID ;echo "已成功关闭${NUM} 个tomcat进程"
 59                 else
 60                         echo  "Tomcat 已经关闭完成!"
 61                 echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
 62                 fi
 63         else
 64                 echo "Tomcat 没有运行"
 65                 echo 1
 66         fi
 67         if  netstat -an | grep 8080 | grep LISTEN >/dev/null;then
 68             PID=`ps -ef  | grep  -v grep  | grep java | awk  '{print $2}'`
 69             #NUM=`ps -ef | grep  -v "color"  | grep tomcat | awk '{print $2}' | wc -l`
 70             echo "关闭失败,即将强制删除tomcat进程!"
 71             sleep 2
 72             pkill tomcat ;sleep 2
 73             if  netstat -an | grep 8080 | grep LISTEN >/dev/null;then
 74                 echo "强制关闭失败,即将再次强制删除tomcat进程!"
 75                 pkill java; sleep 2
 76             fi
 77         fi
 78         }
 79 restart() {
 80         stop
 81         start
 82  }
 83
 84 case "$1" in
 85 start)
 86 start
 87 ;;
 88
 89 stop)
 90 stop
 91 ;;
 92
 93 restart)
 94 restart
 95 ;;
 96
 97 *)
 98 echo $"Usage: $0 {start|stop|restart|status}"
 99 esac

 

posted @ 2021-12-23 21:07  不会跳舞的胖子  阅读(41)  评论(0编辑  收藏  举报