#!/bin/sh
#启动用户
RUNNING_USER=root
SR_HOME=/home/tomcat/tomcat-video-recommend-queue/
APP_MAINCLASS=tomcat-video-recommend-queue/
JAVA_CMD=" sh $SR_HOME/bin/startup.sh "
JAVA_CMD_SHUTDOWN=" sh $SR_HOME/bin/shutdown.sh "
#初始化psid变量（全局）

psid=0
 
checkpid() {
   javaps=`ps aux | grep  $SR_HOME | grep -v grep | grep -v retomcat `
 
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $2}'`
   else
      psid=0
   fi
}


start() {
   checkpid
 
   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_MAINCLASS already started! (pid=$psid)"
      echo "================================"
     else
      echo -n "Starting $APP_MAINCLASS ..."
     # JAVA_CMD=" $JAVA_HOME/bin/java $JAVA_OPTS >${LOGS} 2>&1 &"
      #cd  $APP_HOME/${APP_MAINCLASS}
     su  $RUNNING_USER -c "$JAVA_CMD "
     
      checkpid
         if [ $psid -ne 0 ]; then
              echo "================================"
            echo "(pid=$psid) [OK]"
               echo "================================"
                    else
             echo "[Failed]"
      fi
   fi
}
 
###################################
#(函数)停止程序
###################################
stop() {
   checkpid
 
   if [ $psid -ne 0 ]; then
       echo "================================"
      echo -n "Stopping $APP_MAINCLASS ...(pid=$psid) "
       su   $RUNNING_USER -c "$JAVA_CMD_SHUTDOWN"
      sleep  5
       
      if [ $psid -ne 0 ]; then
         kill  -9  $psid
      fi

      if [ $? -eq 0 ]; then
         echo "[OK]" 
       else
         echo "[Failed]"
      fi
 
      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_MAINCLASS is not running"
      echo "================================"
   fi
}

restart() {

     stop
     start
     info
}        
 
###################################
#(函数)检查程序运行状态
#
#说明：
#1. 首先调用checkpid函数，刷新$psid全局变量
#2. 如果程序已经启动（$psid不等于0），则提示正在运行并表示出pid
#3. 否则，提示程序未运行
###################################
status() {
   checkpid
   echo  "hostname is  $(hostname)" 
   if [ $psid -ne 0 ];  then
       echo "================================"
      echo "$APP_MAINCLASS is running! (pid=$psid)"
       echo "================================"
   else
       echo "================================"
      echo "$APP_MAINCLASS is not running"
       echo "================================"
   fi
}
 
###################################
#(函数)打印系统环境参数
###################################
info() {
   echo "System Information:"
   echo "================================"
   echo `head -n 1 /etc/issue`
   echo `uname -a`
   echo
   echo `$JAVA_HOME/bin/java -version`
   echo
   echo  "==============================="
   echo "SR_HOME=$SR_HOME"
   echo "LOGS_HOME=$LOGS_HOME"
   echo "==============================="
}
 
###################################
#读取脚本的第一个参数($1)，进行判断
#参数取值范围：{start|stop|restart|status|info}
#如参数不在指定范围之内，则打印帮助信息
###################################
case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'status')
     status
     ;;
   'info')
     info
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|status|info}"
     exit 1
;;
esac


 
