Linux下怎么让jar开机自启动

Linux下怎么让jar开机自启动?怎么写shell脚本启动或者其他办法可以实现

把里面的xxxxxx替换成你真是情况。然后把这个脚本注册进系统服务并开机启动. 然后执行sh xxx.sh或者chmod +x xxx.sh -> ./xxx.sh 参数start|stop|force-stop|restart|status

#!/bin/bash
#
#   :: xxxxxxxxxx Startup Script ::
#
### BEGIN INIT INFO
# Provides:          xxxxxxxxxx
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: xxxxxxxxxx
# Description:       xxxxxxxxxx
# chkconfig:         2345 99 01
### END INIT INFO

APP_NAME=xxxxxxxxxx

WORKING_DIR="/xxxxxxxxxx"
CONF_PATH="$WORKING_DIR/config"
LIB_PATH="$WORKING_DIR/lib"
APP_PATH="$WORKING_DIR/www"
APP_JAR="$APP_PATH/xxxxxxxxxx.jar"

LOG_PATH="/xxxxxxxxxx"
STOP_WAIT_TIME=30

LOG_FILE="$LOG_PATH/startup.log"
PID_FILE="$WORKING_DIR/${APP_NAME}.pid"

JAVA_HOME=/xxxxxxxxxx
JAVA_OPTS="-server -Xms1024m -Xmx1024m  -XX:+UseParallelGC -Dfile.encoding=utf-8"
RUN_ARGS=

if [[ -d "$LIB_PATH" ]]; then
    CLASS_PATH=$(find "$LIB_PATH" -name '*.jar' -printf '%p:' | sed 's/:$//'):"${APP_JAR}":"${CONF_PATH}"
else
    CLASS_PATH="${APP_JAR}":"${CONF_PATH}"
fi

MAIN_CLASS=org.springframework.boot.loader.JarLauncher

arguments=($JAVA_OPTS -classpath $CLASS_PATH $MAIN_CLASS $RUN_ARGS)

# ANSI Colors
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }

# Utility functions
checkPermissions() {
  touch "$PID_FILE" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; return 4; }
  mkdir -p "$LOG_PATH" &> /dev/null || { echoRed "Operation not permitted (cannot log path)"; return 4; }
  touch "$LOG_FILE" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; return 4; }
}

isRunning() {
  ps -p "$1" &> /dev/null
}

action="$1"

# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
    javaexe="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
    javaexe=$(type -p java)
elif [[ -x "/usr/bin/java" ]];  then
    javaexe="/usr/bin/java"
else
    echo "Unable to find Java"
    exit 1
fi


# Action functions
start() {
  if [[ -f "$PID_FILE" ]]; then
    pid=$(cat "$PID_FILE")
    isRunning "$pid" && { echoYellow "Already running [$pid]"; return 0; }
  fi
  do_start "$@"
}

do_start() {
  checkPermissions || return $?
  echo "$javaexe ${arguments[@]}"
  "$javaexe" "${arguments[@]}" >> "$LOG_FILE" 2>&1 &
  pid=$!
  disown $pid
  echo "$pid" > "$PID_FILE"
  [[ -z $pid ]] && { echoRed "Failed to start"; return 1; }
  echoGreen "Started [$pid]"
}

stop() {
  [[ -f $PID_FILE ]] || { echoYellow "Not running (pid file not found)"; return 0; }
  pid=$(cat "$PID_FILE")
  isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$PID_FILE"; return 0; }
  do_stop "$pid" "$PID_FILE"
}

do_stop() {
  kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; }
  for i in $(seq 1 $STOP_WAIT_TIME); do
    isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; }
    [[ $i -eq STOP_WAIT_TIME/2 ]] && kill "$1" &> /dev/null
    sleep 1
  done
  echoRed "Unable to kill process $1";
  return 1;
}

force_stop() {
  [[ -f $PID_FILE ]] || { echoYellow "Not running (pidfile not found)"; return 0; }
  pid=$(cat "$PID_FILE")
  isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$PID_FILE"; return 0; }
  do_force_stop "$pid" "$PID_FILE"
}

do_force_stop() {
  kill -9 "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; }
  for i in $(seq 1 $STOP_WAIT_TIME); do
    isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; }
    [[ $i -eq STOP_WAIT_TIME/2 ]] && kill -9 "$1" &> /dev/null
    sleep 1
  done
  echoRed "Unable to kill process $1";
  return 1;
}

restart() {
  stop && start
}

status() {
  [[ -f "$PID_FILE" ]] || { echoRed "Not running"; return 3; }
  pid=$(cat "$PID_FILE")
  isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 1; }
  echoGreen "Running [$pid]"
  return 0
}

# Call the appropriate action function
case "$action" in
start)
  start "$@"; exit $?;;
stop)
  stop "$@"; exit $?;;
force-stop)
  force_stop "$@"; exit $?;;
restart)
  restart "$@"; exit $?;;
status)
  status "$@"; exit $?;;
*)
  echo "Usage: $0 {start|stop|force-stop|restart|status}"; exit 1;
esac

exit 0