侧边栏壁纸
  • 累计撰写 92 篇文章
  • 累计创建 35 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

java启动shell脚本

天明
2023-10-25 / 0 评论 / 0 点赞 / 39 阅读 / 2647 字 / 正在检测是否收录...
#!/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

0

评论区