跳至主要內容

java服务相关脚本

bug君小于 1 分钟

1. 服务启动

说明

  1. 需要修改 jar包名称

  2. 授权 chmod +x start.shopen in new window

  3. 使用:

    ./start start 启动

    ./start stop 停止服务

    ./start restart 重启服务

  4. 启动、停止、重启都会都去 pid.txt 文件中的pid

  5. 不记录日志

#!/bin/bash

JAR_FILE="mdews-1.0-SNAPSHOT.jar"
PID_FILE="pid.txt"

check_process() {
  local pid=$1
  kill -0 $pid 2>/dev/null
  return $?
}

case "$1" in
  start)
    if [ -f $PID_FILE ]; then
      PID=$(cat $PID_FILE)
      if check_process $PID; then
        echo "Application is already running with PID: $PID"
        exit 1
      else
        echo "Stale PID file found. Cleaning up."
        rm $PID_FILE
      fi
    fi

    nohup java -jar $JAR_FILE > /dev/null 2>&1 &
    echo $! > $PID_FILE
    echo "Application started. PID: $(cat $PID_FILE)"
    ;;
  stop)
    if [ -f $PID_FILE ]; then
      PID=$(cat $PID_FILE)
      if check_process $PID; then
        kill $PID
        rm $PID_FILE
        echo "Application stopped."
      else
        echo "No running process found. Removing stale PID file."
        rm $PID_FILE
      fi
    else
      echo "No existing PID file. Application may not be running."
    fi
    ;;
  restart)
    if [ -f $PID_FILE ]; then
      ./start.sh stop
    fi
    ./start.sh start
    ;;
  *)
    echo "Usage: ./start.sh {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

上次编辑于: