跳至主要內容

服务启动脚本

引领潮流大约 1 分钟serverenv

scp上传免密

ry.sh

#!/bin/sh
# shellcheck disable=SC2006
# shellcheck disable=SC2126
# shellcheck disable=SC2046
# shellcheck disable=SC2107

# author chentong
# date: 2024-04-25
# ./ry.sh start 启动
# ./ry.sh stop 停止
# ./ry.sh restart 重启
# ./ry.sh status 状态
AppName=ruoyi-admin.jar

# JVM参数
JVM_OPTS="-Dname=$AppName  -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps  -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"

# PORT DEFAULT 8080
PORT=8080

banner() {
  echo "////////////////////////////////////////////////////////////////////"
  echo "//                          _ooOoo_                               //"
  echo "//                         o8888888o                              //"
  echo "//                         88" . "88                                //"
  echo "//                         (| ^_^ |)                              //"
  echo "//                         O\  =  /O                              //"
  echo "//                   /  \\|||  :  |||//  \                         //"
  echo "//                  /  _||||| -:- |||||-  \                       //"
  echo "//                  |   | \\\  -  /// |   |                         //"
  echo "//                  | \_|  ''\---/''  |   |                       //"
  echo "//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //"
  echo "//             佛祖保佑       永不宕机      永无BUG               //"
  echo "////////////////////////////////////////////////////////////////////"
}

# error log
error() {
  echo "\033[0;31m$1\033[0m"
}

# info log
info() {
  echo "\033[0;34m$1\033[0m"
}

if [ "$1" = "" ]; then
  echo $(error "  please input command:  ") $(info "   {start|stop|restart|status|port|banner}  ")
  exit 1
fi

if [ "$1" = "port" -a $# = 2 ]; then
  PORT=$2
fi

# get pid
pid() {
  PID=$(ps -ef | grep java | grep $AppName | grep -v grep | awk '{print $2}')
  echo "$PID"
}

start() {
  banner
  PID=$(pid)
  if [ x"$PID" != x"" ]; then
    info "START $AppName is running..."
  else
    #$JVM_OPTS is not used
    nohup java -jar $AppName $JVM_OPTS >/dev/null 2>&1 &
    info "START $AppName success..."
  fi
}

stop() {
  error "STOP $AppName"
  PID=$(pid)
  if [ x"$PID" != x"" ]; then
    kill -TERM $PID
    error "STOP $AppName (pid:$PID) exiting..."
    while [ x"$PID" != x"" ]; do
      sleep 1
      # repeat get pid
      PID=$(pid)
    done
    error "STOP $AppName stopped"
  else
    error "STOP $AppName already stopped."
  fi
}

restart() {
  info "RESTART $AppName begin"
  stop
  sleep 2
  start
  status
  info "RESTART $AppName end"
}

status() {
  PID=$(pid)
  if [ x"$PID" != x"" ]; then
    info "STATUS $AppName is running..."
  else
    error "STATUS $AppName is not running..."
  fi
}

# kill by port
port() {
  lsof -i:$PORT | grep LISTEN | awk '{print $2}' | xargs kill -9
  error "PORT $PORT is killed"
  status
}

# shell main method
case $1 in
start)
  start;;
stop)
  stop;;
restart)
  restart;;
status)
  status;;
port)
  port;;
banner)
  banner;;
*) ;;

esac