售前咨询
技术支持
渠道合作

Tomcat启动脚本

记录一个比较好的tomcat启动脚本,截取《OneinStack》,修改如下两个参数即可用。

使用之前修改下面2个参数:

  1. #Location of JAVA_HOME (bin files)
  2. export JAVA_HOME=/usr/java/jdk1.7.0_80    #jdk路径
  3. #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
  4. CATALINA_HOME=/usr/local/tomcat    #tomcat路径  
  5. #TOMCAT_USER is the default user of tomcat
  6. TOMCAT_USER=www    #tomcat启动用户,需要shell权限

tomcat启动、停止、重启,如下图:

脚本内容如下:

  1. #!/bin/bash
  2. #
  3. # chkconfig: – 95 15 
  4. # description: Tomcat start/stop/status script
  5. #Location of JAVA_HOME (bin files)
  6. export JAVA_HOME=
  7. #Add Java binary files to PATH
  8. export PATH=$JAVA_HOME/bin:$PATH
  9. #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
  10. CATALINA_HOME=/usr/local/tomcat
  11. #TOMCAT_USER is the default user of tomcat
  12. TOMCAT_USER=www
  13. #TOMCAT_USAGE is the message if this script is called without any options
  14. TOMCAT_USAGE=”Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}”
  15. #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
  16. SHUTDOWN_WAIT=20
  17. tomcat_pid() {
  18.         echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s ” “|cut -d” ” -f2`
  19. }
  20. start() {
  21.   pid=$(tomcat_pid)
  22.   if [ -n “$pid” ];then
  23.     echo -e “\e[00;31mTomcat is already running (pid: $pid)\e[00m”
  24.   else
  25.     echo -e “\e[00;32mStarting tomcat\e[00m”
  26.     if [ `user_exists $TOMCAT_USER` = “1” ];then
  27.       su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
  28.     else
  29.       $CATALINA_HOME/bin/startup.sh
  30.     fi
  31.     status
  32.   fi
  33.   return 0
  34. }
  35. status(){
  36.   pid=$(tomcat_pid)
  37.   if [ -n “$pid” ];then
  38.     echo -e “\e[00;32mTomcat is running with pid: $pid\e[00m”
  39.   else
  40.     echo -e “\e[00;31mTomcat is not running\e[00m”
  41.   fi
  42. }
  43. stop() {
  44.   pid=$(tomcat_pid)
  45.   if [ -n “$pid” ];then
  46.     echo -e “\e[00;31mStoping Tomcat\e[00m”
  47.         $CATALINA_HOME/bin/shutdown.sh
  48.     let kwait=$SHUTDOWN_WAIT
  49.     count=0;
  50.     until [ `ps -p $pid | grep -c $pid` = ‘0’ ] || [ $count -gt $kwait ]
  51.     do
  52.       echo -n -e “\e[00;31mwaiting for processes to exit\e[00m\n”;
  53.       sleep 1
  54.       let count=$count+1;
  55.     done
  56.     if [ $count -gt $kwait ];then
  57.       echo -n -e “\n\e[00;31mkilling processes which didn’t stop after $SHUTDOWN_WAIT seconds\e[00m”
  58.       kill -9 $pid
  59.     fi
  60.   else
  61.     echo -e “\e[00;31mTomcat is not running\e[00m”
  62.   fi
  63.   return 0
  64. }
  65. user_exists(){
  66.   if id -u $1 >/dev/null 2>&1; then
  67.     echo “1”
  68.   else
  69.     echo “0”
  70.   fi
  71. }
  72. case $1 in
  73.         start)
  74.           start
  75.         ;;
  76.         stop)
  77.           stop
  78.         ;;
  79.         restart)
  80.           stop
  81.           start
  82.         ;;
  83.         status)
  84.       status
  85.         ;;
  86.         *)
  87.       echo -e $TOMCAT_USAGE
  88.         ;;
  89. esac
  90. exit 0

上一篇:

下一篇:

相关新闻

 

领取优惠
免费预约

申请试用SSL证书

提交成功!

咨询客服