@ -9,20 +9,34 @@
# :: Spring Boot Startup Script ::
# :: Spring Boot Startup Script ::
#
#
[[ -n "$DEBUG" ]] && set -x
WORKING_DIR="$(pwd)"
WORKING_DIR="$(pwd)"
PID_FOLDER="/var/run"
[[ -n "$JARFILE" ]] && jarfile="$JARFILE"
USER_PID_FOLDER="/tmp"
[[ -n "$APP_NAME" ]] && identity="$APP_NAME"
LOG_FOLDER="/var/log"
[[ -z "$PID_FOLDER" ]] && PID_FOLDER="/var/run"
USER_LOG_FOLDER="/tmp"
[[ -z "$LOG_FOLDER" ]] && LOG_FOLDER="/var/log"
! [[ -x "$PID_FOLDER" ]] && PID_FOLDER="/tmp"
! [[ -x "$LOG_FOLDER" ]] && LOG_FOLDER="/tmp"
# Setup defaults
# Setup defaults
[[ -z "$mode" ]] && mode ="{{mode:auto}}" # modes are "auto", "service" or "run"
[[ -z "$MODE" ]] && MODE ="{{mode:auto}}" # modes are "auto", "service" or "run"
# ANSI Colors
# ANSI Colors
echoRed() { echo $'\e[0;31m'$1$'\e[0m'; }
echoRed() { echo $'\e[0;31m'$1$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'$1$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'$1$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'$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)"; exit 1; }
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; exit 1; }
}
isRunning() {
ps -p $1 &> /dev/null
}
# Follow symlinks to find the real jar and detect init.d script
# Follow symlinks to find the real jar and detect init.d script
cd $(dirname "$0")
cd $(dirname "$0")
[[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "$0")
[[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "$0")
@ -36,36 +50,36 @@ cd "$WORKING_DIR"
# Determine the script mode
# Determine the script mode
action="run"
action="run"
if [[ "$mode" == "auto" && -n "$init_script" ]] || [[ "$mode " == "service" ]]; then
if [[ "$MODE" == "auto" && -n "$init_script" ]] || [[ "$MODE " == "service" ]]; then
action="$1"
action="$1"
shift
shift
fi
fi
# Create an identity for log/pid files
# Create an identity for log/pid files
if [[ -n "$init_script" ]]; then
if [[ -z "$identity" ]]; then
identity="${init_script}"
if [[ -n "$init_script" ]]; then
else
identity="${init_script}"
jar_folder=$(dirname "$jarfile")
else
identity=$(basename "${jarfile%.*}")_${jar_folder//\//}
jar_folder=$(dirname "$jarfile")
identity=$(basename "${jarfile%.*}")_${jar_folder//\//}
fi
fi
fi
# Build the pid and log filenames
# Build the pid and log filenames
if [[ -n "$init_script" ]]; then
if [[ "$identity" == "$init_script" ]] || [[ "$identity" == "$APP_NAME" ]]; then
pid_file="$PID_FOLDER/${identity}/${identity}.pid"
PID_FOLDER="$PID_FOLDER/${identity}"
log_file="$LOG_FOLDER/${identity}.log"
else
pid_file="$USER_PID_FOLDER/${identity}.pid"
log_file="$USER_LOG_FOLDER/${identity}.log"
fi
fi
pid_file="$PID_FOLDER/${identity}.pid"
log_file="$LOG_FOLDER/${identity}.log"
# Determine the user to run as
# Determine the user to run as if we are root
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
# Find Java
# Find Java
if type -p java 2>&1> /dev/null; then
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
javaexe=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
javaexe="$JAVA_HOME/bin/java"
javaexe="$JAVA_HOME/bin/java"
elif type -p java 2>&1> /dev/null; then
javaexe=java
elif [[ -x "/usr/bin/java" ]]; then
elif [[ -x "/usr/bin/java" ]]; then
javaexe="/usr/bin/java"
javaexe="/usr/bin/java"
else
else
@ -76,16 +90,6 @@ fi
# Build actual command to execute
# Build actual command to execute
command="$javaexe -jar -Dsun.misc.URLClassPath.disableJarChecking=true $jarfile $@"
command="$javaexe -jar -Dsun.misc.URLClassPath.disableJarChecking=true $jarfile $@"
# Utility functions
checkPermissions() {
touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; exit 1; }
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; exit 1; }
}
isRunning() {
ps -p $1 &> /dev/null
}
# Action functions
# Action functions
start() {
start() {
if [[ -f "$pid_file" ]]; then
if [[ -f "$pid_file" ]]; then
@ -94,9 +98,9 @@ start() {
fi
fi
pushd $(dirname "$jarfile") > /dev/null
pushd $(dirname "$jarfile") > /dev/null
if [[ -n "$run_user" ]]; then
if [[ -n "$run_user" ]]; then
mkdir "$PID_FOLDER/${identity} " &> /dev/null
mkdir "$PID_FOLDER" &> /dev/null
checkPermissions
checkPermissions
chown "$run_user" "$PID_FOLDER/${identity} "
chown "$run_user" "$PID_FOLDER"
chown "$run_user" "$pid_file"
chown "$run_user" "$pid_file"
chown "$run_user" "$log_file"
chown "$run_user" "$log_file"
su -c "$command &> \"$log_file\" & echo \$!" $run_user > "$pid_file"
su -c "$command &> \"$log_file\" & echo \$!" $run_user > "$pid_file"
@ -115,6 +119,7 @@ start() {
stop() {
stop() {
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; exit 1; }
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; exit 1; }
pid=$(cat "$pid_file")
pid=$(cat "$pid_file")
rm -f "$pid_file"
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; exit 1; }
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; exit 1; }
kill -HUP $pid &> /dev/null || { echoRed "Unable to kill process ${pid}"; exit 1; }
kill -HUP $pid &> /dev/null || { echoRed "Unable to kill process ${pid}"; exit 1; }
for i in $(seq 1 20); do
for i in $(seq 1 20); do