Linux 启动停止重启jar包shell脚本

#!/bin/bash

# 设置JAR包的路径
JAR_PATH="/deploy/simple/simple.jar"

# 设置Java虚拟机参数
JAVA_OPTS="-Xmx512m -Xms256m"

start() {
  # 日志文件存放位置
  LOG_PATH="/deploy/simple/simple.log"

  echo "Starting the application..."
  nohup java $JAVA_OPTS -jar $JAR_PATH > "$LOG_PATH" 2>&1 &
  echo "Application started. Logs are in: $LOG_PATH"
}

stop() {
  echo "Stopping the application..."
  PID=$(ps aux | grep $JAR_PATH | grep -v grep | awk '{print $2}')
  if [ -z "$PID" ]; then
    echo "Application is not running."
  else
    kill -9 $PID
    echo "Application stopped."
  fi
}

restart() {
  stop
  start
}

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

exit 0

保存脚本赋予执行权限:

chmod +x simple.sh

执行、停止或者重启

./simple.sh start

你可能感兴趣的:(碎片化小知识点,linux,jar,java)