06月07, 2017

java启动shell脚本

#!/bin/bash
JAVA=/www/env/java/bin/java
JAVA_OPTS=" -jar -Dspring.profiles.active=prod"
NAME=server_jar_name
CONSOLE_LOG_FILE=/www/logs/${NAME}.log
APP_HOME=/www/server/${NAME}
APP_CMD=${NAME}.jar
#################
PIDS=0
function getpid(){
  PIDS=$(ps ax | grep -i ${NAME} | grep java | grep -v grep | awk '{print $1}')
  if [ ! $PIDS ];then
      PIDS=0
  fi
}
function status(){
getpid
if [ $PIDS -ne 0 ]; then
  echo "$NAME is running! (pid=$PIDS)"
else
  echo "$NAME is not running"
fi
}
function stop(){
getpid
if [ -z "$PIDS" ]; then
  echo "No ${NAME} to stop"
  exit 1
else
  echo "stop ${NAME} server"
  kill -s TERM $PIDS
  sleep 5
  getpid
  if [ $PIDS -ne 0 ]; then
       echo "Stop $NAME [Failed] (pid=$PIDS)"
  else
       echo "Stop $NAME success [OK]"
  fi
fi
}
function start(){
if [ $PIDS -ne 0 ]; then
  echo "================================"
  echo "warn: $APP_NAME already started! (pid=$PIDS)"
  echo "================================"
else
  echo -n "Starting $NAME ..."
  nohup $JAVA $JAVA_OPTS $APP_HOME/$APP_CMD "$NAME" > "$CONSOLE_LOG_FILE" 2>&1 < /dev/null &
  sleep 5
  getpid

  if [ $PIDS -ne 0 ]; then
       echo "(pid=$PIDS) [OK]"
       echo "log on $CONSOLE_LOG_FILE"
  else
       echo "[Failed]"
  fi
fi
}

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     sleep 3
     start
     ;;
   'status')
     status
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|status}"
     exit 1
esac
exit 0

本文链接:https://blog.jnliok.com/post/java-application-start-shell.html

-- EOF --

Comments