学习tomcat之通过shell批量管理多个tomcat

来源:互联网 发布:织梦小说源码 编辑:程序博客网 时间:2024/06/06 03:42
#!/bin/bash# Apache Tomcat daemon# chkconfig: 345 10 10# description: Apache Tomcat daemon# processname: tomcatecho "  ______                           __   "echo " /_  __/___  ____ ___  _________ _/ /_  "echo "  / / / __ \/ __  __ \/ ___/ __  / __/  "echo " / / / /_/ / / / / / / /__/ /_/ / /_    "echo "/_/  \____/_/ /_/ /_/\___/\__,_/\__/    "echo "                                        "#定义多个tomcat的总目录,/usr/share目录下有tomcat-app、tomcat-service、tomcat-web 三个tomcat实例。tom="/usr/share/"#定义启动脚本路径startup_bin="bin/startup.sh"#定义tomcat的启动方式,启动方式为tomcat.sh p1 start类似的命令usage="{service|app|web|all} {start|stop|restart|status}"dev="/autobuild/logs/tomcat_opt.log"#定义如何启动tomcat,在此我们是通过个数tomcat以及前面定义的tomcat的命令,来操作tomcat#judge $1 $2 whether nullif [ "$1" == "" -o "$2" == "" ];then    echo     echo "Usage: $0 [servername] [options]"     echo     echo "servername:"     echo "app                      the server of tomcat-app"     echo "service                      the server of tomcat-service"     echo "web                      the server of tomcat-web"     echo "all                      all of server include[tomcat-app|tomcat-service|tomcat-web]"     echo     echo "options:"     echo "start                        start server"     echo "stop                     stop server"     echo "restart                      restart server"     echo "status                       view server status"     echo     echo "Example:"     echo "$0 all restart          restart all server"     echo "$0 all stop         stop all server"     echo "$0 app start            start tomcat-app server"     echo "$0 service restart          restart tomcat-service server"     echo "$0 all status           view all server status"     echo "......"    echoexit 1fi#judge $1case $1 in   "app")    tomcats="tomcat-app";;   "web")    tomcats="tomcat-web";;   "service")    tomcats="tomcat-service";;   "all")    tomcats="tomcat-service tomcat-app tomcat-web";;   *)   echo "Usage: $0 $usage";;esac#定义tomcat的启动#define start functiontomcatstart() {for i in $tomcats  do  tom_home="$tom$i"  run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")if [ "${run_status}X" != "X" ];thenecho "node $i is already running..."else${tom_home}/${startup_bin} &>$devecho "node $i starting,Please wait 2s..."sleep 2fidone}#定义tomcat的关闭#define stop functiontomcatstop() {for j in $tomcatsdotom1_home="$tom$j"tomcat_pid=$(ps -ef | grep ${tom1_home} | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $2}')if [ "${tomcat_pid}X" == "X" ];thenecho "node $j is not running..."elsekill -9 ${tomcat_pid} & >$devecho "node $j stopping,Please wait 1s..."sleep 1echo "delte node $j cache,Please wait 1s..."rm -rf ${tom1_home}/work/*fidone}#定义tomcat的重启#define restart functiontomcatrestart() {for m in $tomcatsdotom2_home="$tom$m"run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")if [ "${run2_status}X" == "X" ];thenecho "node $m is not running..."${tom2_home}/${startup_bin} &>$devecho "node $m starting,Please wait 2s..."sleep 2elseps -ef | grep ${tom2_home} | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $2}'| xargs kill -9 >$devecho "node $m stopping,Please wait 2s..."sleep 1${tom2_home}/${startup_bin} &>$devecho "node $m starting,Please wait 2s..."sleep 2fidone}#定义tomcat的状态#define status functiontomcatstatus() {for n in $tomcatsdotom3_home="$tom$n"run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")if [ "${run3_status}X" == "X" ];thenecho "node $n is not running..."elseecho "node $n is running"fidone}#judge $2case $2 in    "start")    tomcatstart;;    "stop")    tomcatstop;;   "restart")   tomcatrestart;;   "status")   tomcatstatus;;   *)   echo "Usage: $0 $usage";;esac
0 0
原创粉丝点击