生產環境中使用腳本實現tomcat start|status|stop|restart


一、在實際生產環境中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         
vim tomcatd.sh

 

  放心這腳本是我工作中自己寫的啟動腳本,已經測試過。如有疑問請看腳本執行情況:

   請看:

  1、首先來看下服務是否啟動

   

       腳本實現了查看服務是否啟動;

  2、再次執行腳本看看能否啟動多個服務

  

  注意紅色部分已經提示服務已經啟動不需要在執行命令了,而且查看進程只有一個剛啟動服務。

  3.停止服務還有僵屍進程嗎?:

  

  停止后服務沒有了?

  4.那我們在服務沒有起來的情況下,我們想重啟服務呢?

  

  沒有啟動服務情況下執行restart會提示,而且服務也不會啟動。

  5、啟動服務:

  

  服務又起來了。

  6、如果是別人不知道這個服務后面要給參數或者輸錯了參數start|stop|restart|status才能啟動那么會出現什么效果呢?

  

  

  OK到現在所有的功能已經實現而且跟系統的 啟動方法類似了。只是系統實現的是、service  XXXX  start|stop|restart|status或者是etc/init.d/xxx   start|stop|restart|status

如何才能實現和系統一模一樣的功能呢?

三、使用service  tomcatd  start啟停或者是/etc/init.d/tomcatd  start

  1、將腳本文件拷貝至/etc/init.d/

    cp  tomcatd.sh  /etc/init.d/tomcatd

    chmod +x /etc/init.d/tomcatd 加可執行權限。

    默認已經可執行service  tomcatd  start|stop|restart|status

  2、將tomcatd加入chkconfig開機自啟動。

    # chkconfig: 2345 92 92  請注意以上腳本文件中有這一行表示2345啟動號碼是92

    從哪兒來的?

    使用ls  -l   /etc/rc3.d 可查看

    

    注意:最好從中選去沒有的號碼如:

    

    已經有27了那么不能使用27。

     

    93  94  92 (原本沒有之后加入了tomcatd)表示沒有用可用

    所以使用92

  3、使用chkconfig  --add   tomcatd  加入

    

    不報錯表示成功;

  4、檢查一下:chkconfig  --list |grep  tomcatd

    

  

  

  ok  成功!

  以上是簡單的腳本實現功能,高手繞道,不好勿噴,適合初學以及腳本學習者,原本想在腳本中增加注釋,但是自己想偷懶如有疑問可留言。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM