Skip to content

Instantly share code, notes, and snippets.

@programingjd
Last active August 7, 2016 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programingjd/19350d78956843ca3ac63facf37f3419 to your computer and use it in GitHub Desktop.
Save programingjd/19350d78956843ca3ac63facf37f3419 to your computer and use it in GitHub Desktop.
Bash script for start/stop/status/run operations on a server implemented in java.
#!/usr/bin/env bash
################################################################################
WEBROOT=/home/admin/www/ezpzjapanez.com
CERT=/home/admin/certs/certificates/ezpzjapanez.com.p12
################################################################################
# Directory containing this script
DIR=$(dirname $(readlink /proc/$$/fd/255))
# Canonical Java path
JAVA_HOME=${JAVA_HOME:-}
if [[ -z "${JAVA_HOME}" ]]; then _JAVA=java; else _JAVA="${JAVA_HOME}/bin/java"; fi
JAVA="$(readlink -e "$(type -P ${_JAVA})")"
JAVA="${JAVA:-${_JAVA}}"
DEBUG=""
ARG=""
if [ "$1" == "--debug" ]; then
DEBUG="true"
ARG="$2"
else
if [ "$2" == "--debug" ]; then
DEBUG="true"
ARG="$1"
else
ARG="$1"
fi
fi
# JVM arg for remote debugging
if [[ "${DEBUG}" == "true" ]]; then
JDWP="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
else
JDWP=
fi
# Jar
for JAR in ${DIR}/build/libs/*.jar; do break; done
# BootClasspath Jar
for BOOT in ${DIR}/build/boot/*.jar; do break; done
if [[ -n "${BOOT}" ]]; then
CMD="${JAVA} -Xbootclasspath/p:${BOOT} ${JDWP} -jar ${JAR} ${WEBROOT} ${CERT}"
CMD_ALT="${JAVA} -Xbootclasspath/p:${BOOT} -jar ${JAR} ${WEBROOT} ${CERT}"
else
CMD="${JAVA} ${JDWP} -jar ${JAR} ${WEBROOT} ${CERT}"
CMD_ALT="${JAVA} -jar ${JAR} ${WEBROOT} ${CERT}"
fi
# Find process
function findProcess() {
PID=
for PROC in /proc/*/; do
# Java process
EXE="$(type -P ${PROC}exe)"
EXE="${EXE:-${PROC}exe}"
EXE="$(readlink -e "${EXE}")"
if [[ "${EXE}" == "${JAVA}" ]]; then
# Started from this directory
CMD_LINE="$(cat "${PROC}cmdline" | tr "\0" " ")"
if [[ "${CMD} " == "${CMD_LINE}" || "${CMD_ALT} " == "${CMD_LINE}" ]]; then
PROC=${PROC#/*/}
PID=${PROC%/}
break
fi
fi
done
}
findProcess
function start() {
if [[ -n "${PID}" ]]; then
echo "Server is already running" >&2
false
else
echo -n "Starting server..." >&2
cat "${DIR}"/server.log >> "${DIR}"/server.old.log
${CMD} > "${DIR}/server.log" 2>&1 &
echo "Done" >&2
true
fi
}
function run() {
if [[ -n "${PID}" ]]; then
tail --follow --lines=0 --pid=${PID} ${DIR}/server.log
else
cat ${DIR}/server.log >> ${DIR}/server.old.log
${CMD} | tee "${DIR}/server.log" 2>&1
fi
}
function stop() {
if [[ -n "${PID}" ]]; then
echo -n "Stopping server..." >&2
$(kill -9 ${PID} 2>/dev/null) && (echo "Done" && true) || (echo "Failed" && false)
else
echo "Server is not running" >&2
true
fi
}
function restart() {
stop && (findProcess; start)
}
function status() {
if [[ -n "${PID}" ]]; then
echo "Server is running" >&2
true
else
echo "Server is not running" >&2
false
fi
}
case "${ARG}" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
run)
run
;;
*)
echo "Usage: $0 {start|stop|status|run} [--debug]"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment