一、在实际生产环境中tomcat启动是在bin目录下采用自带脚本startup.sh启动;使用shutdown.sh关闭。如下图:
再如果对于新手来讲在不知道路径情况下重启是一件头痛的事情(注意没有reload,所以重启只能shutdown.sh在startup.sh);而且这里还有一个坑等着:
什么坑呢? 如图:
tomcat服务是启动成功了的。那么我想停止服务用shutdown.sh,会出现什么呢?
进程还在而且成为了僵尸进程,万恶啊?居然关不了,终极方法kill -9 进程号。试试?
终于干掉了。
再次启动:
OK已经成功;还有一个坑在?请看,服务已经启动,如果我再次执行startup.sh会出现什么呢?
请看红色,启动命令能正常执行,而且还能启动一个服务意思是有双服务双进程。万恶啊……
好问题来了我们如何简单命令的启动tomcat服务呢?如何让服务启动了再次执行启动就不会执行了呢?ok。shell脚本实现:
二、脚本实现
1 #!/bin/bash 2 ############################################# 3 # this script is created by xuxuedong. # 4 # e_mail:365686746@qq.com # 5 # qqinfo:365686746 # 6 # This is server start to chkconfig. # 7 # version:1.1 # 8 ############################################# 9 # chkconfig: 2345 92 92 10 # description: Saves and restores system entropy pool for \ 11 # higher quality random number generatio 12 13 . /etc/init.d/functions 14 #set env 15 export PATH=$PATH:/bin:/sbin:/usr/sbin 16 export LANG="zh_CN.GB18030" 17 18 # Require root to run this script. 19 if [[ "$(whoami)" != "root" ]]; then 20 echo "Please run this script as root." >&2 21 exit 1 22 fi 23 24 # Source function library. 25 #. /etc/init.d/functions 26 if [ ! -f /opt/software/apache-tomcat-7.0.72/bin/startup.sh ] 27 then 28 echo "tomcat is not exit.please install." 29 exit 1 30 fi 31 #This is a function for start tomcat 32 function start(){ 33 if [ `ps -ef |grep java|grep -v grep|grep -v sh|wc -l` -eq 0 ] 34 then 35 /bin/sh /opt/software/apache-tomcat-7.0.72/bin/startup.sh >/dev/null 2>&1 36 [ $? -eq 0 ]&&\ 37 sleep 1 38 action "tmocat start." /bin/true 39 else 40 action "tomcat had been startted." /bin/true 41 exit 3 42 fi 43 } 44 #This is a function for stop tomcat 45 function stop(){ 46 if [ `ps -ef |grep java|grep -v grep|grep -v sh|wc -l` -gt 0 ] 47 then 48 PID=`ps -ef |grep java|grep -v grep|awk '{print $2}'` 49 kill -9 $PID 50 [ $? -eq 0 ]&&\ 51 echo "tomcat is stopping..." 52 sleep 1 53 action "tomcat been stoped." /bin/true 54 else 55 action "tomcat had been stoped." /bin/true 56 exit 4 57 fi 58 } 59 #This is a function for restart tomcat 60 function restart(){ 61 if [ `ps -ef |grep java |grep -v grep|grep -v sh|wc -l` -gt 0 ] 62 then 63 PID1=`ps -ef |grep java|grep -v grep|awk '{print $2}'` 64 kill -9 $PID1 65 [ $? -eq 0 ]&&/bin/sh /opt/software/apache-tomcat-7.0.72/bin/startup.sh >/dev/null 2>&1 66 67 [ $? -eq 0 ]&&echo "tomcat is restarting..." 68 sleep 1 69 action "tomcat is restartted ." /bin/true 70 else 71 action "tomcat is not running,please start." /bin/true 72 exit 5 73 fi 74 } 75 #This is a function for status tomcat 76 function status(){ 77 if [ `ps -ef |grep java |grep -v grep|wc -l` -gt 0 ] 78 then 79 action "tomcat is running." /bin/true 80 else 81 action "tomcat is stopped." /bin/false 82 exit 5 83 fi 84 } 85 case $1 in 86 start) 87 start 88 ;; 89 stop) 90 stop 91 ;; 92 restart) 93 restart 94 ;; 95 status) 96 status 97 ;; 98 99 *) 100 echo "USAG:start|stop|restart|status" 101 esa