Tomcat start/stop script

系统 2299 0

http://blog.valotas.com/2011/05/tomcat-initd-script.html

http://blog.botha.us/sarel/?p=101

      #!/bin/
      
        bash

#

# tomcat7     This shell script takes care of starting and stopping Tomcat

#

# chkconfig: 
      
      - 
      
        80
      
      
        20
      
      
        

#

### BEGIN INIT INFO

# Provides: tomcat7

# Required
      
      -
      
        Start: $network $syslog

# Required
      
      -
      
        Stop: $network $syslog

# Default
      
      -
      
        Start:

# Default
      
      -
      
        Stop:

# Description: Release implementation 
      
      
        for
      
       Servlet 
      
        2.5
      
       and JSP 
      
        2.1
      
      
        

# Short
      
      -
      
        Description: start and stop tomcat

### END INIT INFO



## Source 
      
      
        function
      
      
         library.

#. 
      
      /etc/rc.d/init.d/
      
        functions

export JAVA_HOME
      
      =/usr/java/
      
        default

export JAVA_OPTS
      
      =
      
        "
      
      
        -Dfile.encoding=UTF-8 \
      
      

  -Dcatalina.logbase=/var/log/
      
        tomcat7 \

  
      
      -Dnet.sf.ehcache.skipUpdateCheck=
      
        true
      
      
         \

  
      
      -XX:+
      
        DoEscapeAnalysis \

  
      
      -XX:+
      
        UseConcMarkSweepGC \

  
      
      -XX:+
      
        CMSClassUnloadingEnabled \

  
      
      -XX:+
      
        UseParNewGC \

  
      
      -XX:MaxPermSize=
      
        128m \

  
      
      -Xms512m -Xmx512m
      
        "


      
      export PATH=$JAVA_HOME/
      
        bin:$PATH

TOMCAT_HOME
      
      =/usr/share/
      
        tomcat7

SHUTDOWN_WAIT
      
      =
      
        20
      
      
        



tomcat_pid() {

  
      
      
        echo
      
       `
      
        ps
      
       aux | 
      
        grep
      
       org.apache.catalina.startup.Bootstrap | 
      
        grep
      
       -v 
      
        grep
      
       | 
      
        awk
      
      
        '
      
      
        { print $2 }
      
      
        '
      
      
        `

}



start() {

  pid
      
      =
      
        $(tomcat_pid)

  
      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ] 

  
      
      
        then
      
      
        echo
      
      
        "
      
      
        Tomcat is already running (pid: $pid)
      
      
        "
      
      
        else
      
      
        

    # Start tomcat

    
      
      
        echo
      
      
        "
      
      
        Starting tomcat
      
      
        "
      
      
        

    ulimit 
      
      -n 
      
        100000
      
      
        

    umask 
      
      
        007
      
      

    /bin/
      
        su
      
       -p -s /bin/
      
        sh
      
       tomcat $TOMCAT_HOME/bin/startup.
      
        sh
      
      
        fi
      
      
        





  return 
      
      
        0
      
      
        

}



stop() {

  pid
      
      =
      
        $(tomcat_pid)

  
      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ]

  
      
      
        then
      
      
        echo
      
      
        "
      
      
        Stoping Tomcat
      
      
        "
      
      

    /bin/
      
        su
      
       -p -s /bin/
      
        sh
      
       tomcat $TOMCAT_HOME/bin/shutdown.
      
        sh
      
      
        



    let kwait
      
      =
      
        $SHUTDOWN_WAIT

    count
      
      =
      
        0
      
      
        ;

    
      
      
        until
      
       [ `
      
        ps
      
       -p $pid | 
      
        grep
      
       -c $pid` = 
      
        '
      
      
        0
      
      
        '
      
       ] || [ $count -
      
        gt $kwait ]

    
      
      
        do
      
      
        echo
      
       -n -e 
      
        "
      
      
        \nwaiting for processes to exit
      
      
        "
      
      
        ;

      
      
      
        sleep
      
      
        1
      
      
        

      let count
      
      =$count+
      
        1
      
      
        ;

    
      
      
        done
      
      
        if
      
       [ $count -gt $kwait ]; 
      
        then
      
      
        echo
      
       -n -e 
      
        "
      
      
        \nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds
      
      
        "
      
      
        kill
      
       -
      
        9
      
      
         $pid

    
      
      
        fi
      
      
        else
      
      
        echo
      
      
        "
      
      
        Tomcat is not running
      
      
        "
      
      
        fi
      
      
        

 

  return 
      
      
        0
      
      
        

}




      
      
        case
      
       $
      
        1
      
      
        in
      
      
        

start)

  start

;; 

stop)   

  stop

;; 

restart)

  stop

  start

;;

status)

  pid
      
      =
      
        $(tomcat_pid)

  
      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ]

  
      
      
        then
      
      
        echo
      
      
        "
      
      
        Tomcat is running with pid: $pid
      
      
        "
      
      
        else
      
      
        echo
      
      
        "
      
      
        Tomcat is not running
      
      
        "
      
      
        fi
      
      
        

;; 


      
      
        esac
      
      
            

exit 
      
      
        0
      
    
      #!/bin/
      
        bash

#

# tomcat This shell script takes care of starting and stopping Tomcat

# It also handles killing Tomcat 
      
      
        in
      
      
        case
      
      
         it doesn’t stop gracefully

# It uses a PID 
      
      
        file
      
      
         to determine the process ID so it should work with multiple Tomcat instances on one server

#

# chkconfig: 
      
      - 
      
        80
      
      
        20
      
      
        

#

### BEGIN INIT INFO

# Provides: tomcat

# Required
      
      -
      
        Start: $network $syslog

# Required
      
      -
      
        Stop: $network $syslog

# Default
      
      -Start: 
      
        2
      
      
        3
      
      
        4
      
      
        5
      
      
        

# Default
      
      -Stop: 
      
        0
      
      
        1
      
      
        6
      
      
        

# Description: Tomcat service

# Short
      
      -
      
        Description: start and stop tomcat

### END INIT INFO



## Source 
      
      
        function
      
      
         library.

#. 
      
      /etc/rc.d/init.d/
      
        functions



# Begin configuration section



export JAVA_HOME
      
      =/opt/jdk1.
      
        6.0
      
      
        



# These options are used when Tomcat is started

export CATALINA_OPTS
      
      =”-Xmx512m -XX:-
      
        UseGCOverheadLimit \


      
      -Dcom.sun.management.jmxremote=
      
        true
      
      
         \


      
      -Dcom.sun.management.jmxremote.port=
      
        511
      
      
         \


      
      -Dcom.sun.management.jmxremote.authenticate=
      
        false
      
      
         \


      
      -Dcom.sun.management.jmxremote.ssl=
      
        false
      
      
        ”



export LD_LIBRARY_PATH
      
      =/usr/local/apr/
      
        lib



# Set this to the path where this Tomcat is installed

TOMCAT_HOME
      
      =/opt/
      
        tomcat



# Number of seconds to 
      
      
        wait
      
      
         after nicely requesting stop

SHUTDOWN_WAIT
      
      =
      
        10
      
      
        



# This should be a different 
      
      
        file
      
      
        for
      
      
         each tomcat

CATALINA_PID
      
      =/var/run/
      
        tomcat.pid



RUNASUSER
      
      =
      
        root



# End configuration section



export PATH
      
      =$JAVA_HOME/
      
        bin:$PATH

export CATALINA_PID



start() {

pid
      
      =`
      
        cat
      
       $CATALINA_PID 
      
        2
      
      >/dev/
      
        null
      
      
        `


      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ]


      
      
        then
      
      
        echo
      
      
         “Tomcat is already running (pid: $pid)”


      
      
        else
      
      
        

# Start tomcat


      
      
        echo
      
      
         “Starting tomcat”


      
      
        touch
      
      
         $CATALINA_PID


      
      
        chown
      
      
         $RUNASUSER $CATALINA_PID


      
      /bin/
      
        su
      
       -p -s /bin/
      
        sh
      
       $RUNASUSER $TOMCAT_HOME/bin/catalina.
      
        sh
      
      
         start


      
      
        fi
      
      
        



return 
      
      
        0
      
      
        

}



stop() {

pid
      
      =`
      
        cat
      
       $CATALINA_PID 
      
        2
      
      >/dev/
      
        null
      
      
        `


      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ]


      
      
        then
      
      

/bin/
      
        su
      
       -p -s /bin/
      
        sh
      
       $RUNASUSER $TOMCAT_HOME/bin/catalina.
      
        sh
      
      
         stop


      
      
        echo
      
       -
      
        n “Stopping Tomcat”



let kwait
      
      =
      
        $SHUTDOWN_WAIT

count
      
      =
      
        0
      
      
        ;


      
      
        until
      
       [ `
      
        ps
      
       -p $pid | 
      
        grep
      
       -c $pid` = 
      
        '
      
      
        0
      
      
        '
      
       ] || [ $count -
      
        gt $kwait ]


      
      
        do
      
      
        echo
      
       -
      
        n “.”;


      
      
        sleep
      
      
        1
      
      
        

let count
      
      =$count+
      
        1
      
      
        ;


      
      
        done
      
      
        echo
      
      
         “”




      
      
        if
      
       [ $count -gt $kwait ]; 
      
        then
      
      
        echo
      
      
         “process is still running after $SHUTDOWN_WAIT seconds, killing process”


      
      
        kill
      
      
         $pid


      
      
        sleep
      
      
        3
      
      
        



# 
      
      
        if
      
       it’s still running use 
      
        kill
      
       -
      
        9
      
      
        if
      
       [ `
      
        ps
      
       -p $pid | 
      
        grep
      
       -c $pid` -gt 
      
        '
      
      
        0
      
      
        '
      
       ]; 
      
        then
      
      
        echo
      
       “process is still running, using 
      
        kill
      
       -
      
        9
      
      
      
        kill
      
       -
      
        9
      
      
         $pid


      
      
        sleep
      
      
        3
      
      
        fi
      
      
        fi
      
      
        if
      
       [ `
      
        ps
      
       -p $pid | 
      
        grep
      
       -c $pid` -gt 
      
        '
      
      
        0
      
      
        '
      
       ]; 
      
        then
      
      
        echo
      
      
         “process is still running, I give up”


      
      
        else
      
      
        

# success, delete PID 
      
      
        file
      
      
        rm
      
      
         $CATALINA_PID


      
      
        fi
      
      
        else
      
      
        echo
      
      
         “Tomcat is not running”


      
      
        fi
      
      
        

return 
      
      
        0
      
      
        

}




      
      
        case
      
       $
      
        1
      
      
        in
      
      
        

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

status)

pid
      
      =
      
        $(tomcat_pid)


      
      
        if
      
       [ -n 
      
        "
      
      
        $pid
      
      
        "
      
      
         ]


      
      
        then
      
      
        echo
      
      
         “Tomcat is running with pid: $pid”


      
      
        else
      
      
        echo
      
      
         “Tomcat is not running”


      
      
        fi
      
      
        

;;


      
      
        esac
      
      
        

exit 
      
      
        0
      
    

 

 

 

Tomcat start/stop script


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论