Created
January 4, 2017 01:38
-
-
Save crazycode/fa97545f2380b9413005fe06d722af7b to your computer and use it in GitHub Desktop.
/etc/init.d/spring-rule-gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# . ____ _ __ _ _ | |
# /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ | |
# ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ | |
# \\/ ___)| |_)| | | | | || (_| | ) ) ) ) | |
# ' |____| .__|_| |_|_| |_\__, | / / / / | |
# =========|_|==============|___/=/_/_/_/ | |
# :: Spring Boot Startup Script :: | |
# | |
### BEGIN INIT INFO | |
# Provides: rule-gateway | |
# 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: Spring Boot Application - rule-gateway | |
# Description: Spring Boot Application - rule-gateway | |
# chkconfig: 2345 99 01 | |
### END INIT INFO | |
[[ -n "$DEBUG" ]] && set -x | |
# the application name | |
APP_NAME="rule-gateway" | |
RUN_PROFILE=prod | |
DEPLOY_APP_DIR="/opt/deploy/$APP_NAME" | |
DEPLOY_APP_ROOT="/opt/deploy/$APP_NAME/ROOT" | |
LOG_FOLDER="/opt/logs/$APP_NAME" | |
# Initialize variables that cannot be provided by a .conf file | |
WORKING_DIR="$(pwd)" | |
# shellcheck disable=SC2153 | |
[[ -n "$APP_NAME" ]] && identity="$APP_NAME" | |
cd "$WORKING_DIR" || exit 1 | |
# Initialize PID/LOG locations if they weren't provided by the config file | |
[[ -z "$PID_FOLDER" ]] && PID_FOLDER="/var/run" | |
[[ -z "$LOG_FOLDER" ]] && LOG_FOLDER="/var/log" | |
! [[ -x "$PID_FOLDER" ]] && PID_FOLDER="/tmp" | |
MODE=service | |
# Set up defaults | |
[[ -z "$MODE" ]] && MODE="auto" # modes are "auto", "service" or "run" | |
[[ -z "$USE_START_STOP_DAEMON" ]] && USE_START_STOP_DAEMON="true" | |
# Initialize log file name if not provided by the config file | |
[[ -z "$LOG_FILENAME" ]] && LOG_FILENAME="${identity}.log" | |
JAVA_HOME=/opt/jdk8 | |
PATH=$PATH:$JAVA_HOME/bin | |
# 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): $pid_file"; return 4; } | |
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file): $log_file"; return 4; } | |
} | |
isRunning() { | |
ps -p "$1" &> /dev/null | |
} | |
await_file() { | |
end=$(date +%s) | |
let "end+=10" | |
while [[ ! -s "$1" ]] | |
do | |
now=$(date +%s) | |
if [[ $now -ge $end ]]; then | |
break | |
fi | |
sleep 1 | |
done | |
} | |
# Determine the script mode | |
action="run" | |
if [[ "$MODE" == "auto" && -n "$init_script" ]] || [[ "$MODE" == "service" ]]; then | |
action="$1" | |
shift | |
fi | |
# Build the pid and log filenames | |
mkdir -p "$LOG_FOLDER" &> /dev/null | |
pid_file="$PID_FOLDER/${identity}.pid" | |
log_file="$LOG_FOLDER/$LOG_FILENAME" | |
# Determine the user to run as if we are root | |
# shellcheck disable=SC2012 | |
# [[ $(id -u) == "0" ]] && run_user=$(ls -ld "$DEPLOY_APP_ROOT" | awk '{print $3}') | |
run_user=deploy | |
# 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 | |
# use config server | |
RUN_ARGS="$RUN_ARGS -Dspring.cloud.config.uri=http://localhost:8761/config -Dlogging.path=$LOG_FOLDER" | |
#JAVA_OPTS="$JAVA_OPTS -Xmx=1024m -Xms=1024m" | |
# Build actual command to execute | |
command="$javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -cp $DEPLOY_APP_ROOT $RUN_ARGS org.springframework.boot.loader.WarLauncher --spring.profiles.active=$RUN_PROFILE $*" | |
# 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() { | |
working_dir=$DEPLOY_APP_DIR | |
pushd "$working_dir" > /dev/null | |
if [[ -n "$run_user" ]]; then | |
mkdir -p "$PID_FOLDER" &> /dev/null | |
checkPermissions || return $? | |
chown "$run_user" "$PID_FOLDER" | |
chown "$run_user" "$pid_file" | |
chown "$run_user" "$log_file" | |
chown "$run_user" "$LOG_FOLDER" | |
if [ $USE_START_STOP_DAEMON = true ] && type start-stop-daemon > /dev/null 2>&1; then | |
arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -cp $DEPLOY_APP_ROOT $RUN_ARGS org.springframework.boot.loader.WarLauncher --spring.profiles.active=$RUN_PROFILE "$@") | |
start-stop-daemon --start --quiet \ | |
--chuid "$run_user" \ | |
--name "$identity" \ | |
--make-pidfile --pidfile "$pid_file" \ | |
--background --no-close \ | |
--startas "$javaexe" \ | |
--chdir "$working_dir" \ | |
-- "${arguments[@]}" \ | |
>> /dev/null 2>&1 | |
await_file "$pid_file" | |
else | |
su -s /bin/sh -c "$command >> /dev/null 2>&1 & echo \$!" "$run_user" > "$pid_file" | |
fi | |
pid=$(cat "$pid_file") | |
else | |
checkPermissions || return $? | |
$command >> /dev/null 2>&1 & | |
pid=$! | |
disown $pid | |
echo "$pid" > "$pid_file" | |
fi | |
[[ -z $pid ]] && { echoRed "Failed to start"; return 1; } | |
echoGreen "Started [$pid]" | |
} | |
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_stop "$pid" "$pid_file" | |
} | |
do_stop() { | |
kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; } | |
for i in $(seq 1 60); do | |
isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; } | |
[[ $i -eq 30 ]] && kill "$1" &> /dev/null | |
sleep 1 | |
done | |
echoRed "Unable to kill process $1"; | |
return 1; | |
} | |
restart() { | |
stop && start | |
} | |
force_reload() { | |
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; return 7; } | |
pid=$(cat "$pid_file") | |
rm -f "$pid_file" | |
isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 7; } | |
do_stop "$pid" "$pid_file" | |
do_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 | |
} | |
run() { | |
pushd "$DEPLOY_APP_DIR" > /dev/null | |
$command | |
result=$? | |
popd > /dev/null | |
return "$result" | |
} | |
# Call the appropriate action function | |
case "$action" in | |
start) | |
start "$@"; exit $?;; | |
stop) | |
stop "$@"; exit $?;; | |
restart) | |
restart "$@"; exit $?;; | |
force-reload) | |
force_reload "$@"; exit $?;; | |
status) | |
status "$@"; exit $?;; | |
run) | |
run "$@"; exit $?;; | |
*) | |
echo "Usage: $0 {start|stop|restart|force-reload|status|run}"; exit 1; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment