Last active
January 2, 2016 13:09
-
-
Save Stanback/8308145 to your computer and use it in GitHub Desktop.
Example RC script that I've used for a Java/Scala/JVM service
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 | |
# | |
# This script provides start/stop/restart/status control | |
# over the JVM-based API service. | |
# | |
# Build Environment | |
BUILD_NAME="your-build-name" | |
BASE_DIR="/home/ubuntu/${BUILD_NAME}-deploy" | |
BUILD_JAR="${BASE_DIR}/current-build.jar" | |
JAVA_OPTS="-Xms256m -Xmx1024m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:ParallelGCThreads=2 -XX:SurvivorRatio=3 -XX:+UseFastAccessorMethods -XX:ThreadPriorityPolicy=42" | |
PID_FILE="${BASE_DIR}/${BUILD_NAME}.pid" | |
LOG_DIR="${BASE_DIR}/logs" | |
PATH="/bin:/usr/bin:/sbin" | |
# Environment | |
export YOUR_ENV_VARIABLE="foo bar baz" | |
do_start() { | |
pid=$(pid_of_proc) | |
status="$?" | |
if [[ ! -f "$BUILD_JAR" ]]; then | |
echo "${BUILD_NAME} JAR file not found" >&2 | |
exit 1 | |
fi | |
if [[ $status -eq 1 || $status -eq 2 ]]; then | |
logfile=$(get_log_file) | |
java $JAVA_OPTS -jar "$BUILD_JAR" >>"$logfile" 2>&1 & | |
status="$?" | |
pid="$!" | |
if [ $status -eq 0 ]; then | |
echo -n $pid > "$PID_FILE" | |
echo "${BUILD_NAME} started (pid: ${pid})" | |
else | |
echo "${BUILD_NAME} could not be started" >&2 | |
fi | |
else | |
echo "${BUILD_NAME} already running (pid: ${pid})" | |
fi | |
return $status | |
} | |
do_stop() { | |
pid=$(pid_of_proc) | |
status="$?" | |
if [ $status -eq 0 ]; then | |
kill -TERM $pid | |
status="$?" | |
if [ $status -eq 0 ]; then | |
rm -f "$PID_FILE" | |
echo "${BUILD_NAME} has been stopped" | |
else | |
echo "${BUILD_NAME} could not be stopped" >&2 | |
fi | |
elif [ $status -eq 3 ]; then | |
echo "${BUILD_NAME} is running but not owned by you" >&2 | |
elif [ $status -eq 2 ]; then | |
echo "${BUILD_NAME} has no PID file in the configured location" >&2 | |
else | |
echo "${BUILD_NAME} is not running" | |
fi | |
return $status | |
} | |
do_reload() { | |
pid=$(pid_of_proc) | |
status="$?" | |
if [ $status -eq 0 ]; then | |
kill -HUP $pid | |
status="$?" | |
if [ $status -eq 0 ]; then | |
echo "${BUILD_NAME} has been reloaded" | |
else | |
echo "${BUILD_NAME} could not be reloaded" >&2 | |
fi | |
elif [ $status -eq 3 ]; then | |
echo "${BUILD_NAME} is running but not owned by you" >&2 | |
elif [ $status -eq 2 ]; then | |
echo "${BUILD_NAME} has no PID file in the configured location" >&2 | |
else | |
echo "${BUILD_NAME} is not running" | |
fi | |
return $status | |
} | |
get_status() { | |
pid=$(pid_of_proc) | |
status="$?" | |
if [[ $status -eq 0 || $status -eq 2 ]]; then | |
echo "${BUILD_NAME} (pid: ${pid}) is running..." | |
return 0 | |
elif [ $status -eq 3 ]; then | |
echo "Could not access PID file for ${BUILD_NAME}" >&2 | |
else | |
echo "${BUILD_NAME} is stopped" | |
fi | |
return $status | |
} | |
pid_of_proc() { | |
if [[ ! -f "$PID_FILE" || ! -r "$PID_FILE" || -z "$PID_FILE" ]]; then | |
# No PID file, not readable, or empty file | |
return 2 | |
fi | |
pid=$(<"$PID_FILE") | |
if $(kill -0 "${pid:-}" 2> /dev/null); then | |
# Program is running and can be terminated | |
echo $pid | |
return 0 | |
elif ps "${pid:-}" >/dev/null 2>&1; then | |
# Program is running but not owned by this user | |
echo $pid | |
return 3 | |
else | |
# Program not running | |
return 1 | |
fi | |
} | |
get_log_file() { | |
symlink=$(ls -l "$BUILD_JAR" | awk '{print $11}') | |
if [[ $symlink =~ builds/([0-9]+)/ ]]; then | |
echo "${LOG_DIR}/${BUILD_NAME}_build-${BASH_REMATCH[1]}.log" | |
else | |
echo "${LOG_DIR}/${BUILD_NAME}.log" | |
fi | |
} | |
case "$1" in | |
start) | |
do_start | |
;; | |
stop) | |
do_stop | |
;; | |
restart) | |
do_stop | |
do_start | |
;; | |
reload) | |
do_reload | |
;; | |
status) | |
get_status | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|status}" >&2 | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll likely be converting this to a supervisor job