Skip to content

Instantly share code, notes, and snippets.

@williamchitto
Forked from cescoffier/my-vertx-application
Created August 1, 2016 14:13
Show Gist options
  • Save williamchitto/5f1f651103fd7e83415515bf79d70d24 to your computer and use it in GitHub Desktop.
Save williamchitto/5f1f651103fd7e83415515bf79d70d24 to your computer and use it in GitHub Desktop.
Vert.x init.d service script
#!/bin/bash
###
# chkconfig: 345 20 80
# description: Vert.x application service script
# processname: java
#
# Installation (CentOS):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# chkconfig --add /etc/init.d/my-vertx-application
# chkconfig my-vertx-application on
#
# Installation (Ubuntu):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# update-rc.d my-vertx-application defaults
#
#
# Usage: (as root)
# service my-vertx-application start
# service my-vertx-application stop
# service my-vertx-application status
#
###
# The directory in which your application is installed
APPLICATION_DIR="/opt/my-vertx-app"
# The fat jar containing your application
APPLICATION_JAR="maven-verticle-3.0.0-fat.jar"
# The application argument such as -cluster -cluster-host ...
APPLICATION_ARGS=""
# vert.x options and system properties (-Dfoo=bar).
VERTX_OPTS=""
# The Java command to use to launch the application (must be java 8+)
JAVA=/opt/java/java/bin/java
# ***********************************************
OUT_FILE="${APPLICATION_DIR}"/out.log
RUNNING_PID="${APPLICATION_DIR}"/RUNNING_PID
# ***********************************************
# colors
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
reset='\e[0m'
echoRed() { echo -e "${red}$1${reset}"; }
echoGreen() { echo -e "${green}$1${reset}"; }
echoYellow() { echo -e "${yellow}$1${reset}"; }
# Check whether the application is running.
# The check is pretty simple: open a running pid file and check that the process
# is alive.
isrunning() {
# Check for running app
if [ -f "$RUNNING_PID" ]; then
proc=$(cat $RUNNING_PID);
if /bin/ps --pid $proc 1>&2 >/dev/null;
then
return 0
fi
fi
return 1
}
start() {
if isrunning; then
echoYellow "The Vert.x application is already running"
return 0
fi
pushd $APPLICATION_DIR > /dev/null
nohup $JAVA $VERTX_OPTS -jar $APPLICATION_JAR $APPLICATION_ARGS > $OUT_FILE 2>&1 &
echo $! > ${RUNNING_PID}
popd > /dev/null
if isrunning; then
echoGreen "Vert.x Application started"
exit 0
else
echoRed "The Vert.x Application has not started - check log"
exit 3
fi
}
restart() {
echo "Restarting Vert.x Application"
stop
start
}
stop() {
echoYellow "Stopping Vert.x Application"
if isrunning; then
kill `cat $RUNNING_PID`
rm $RUNNING_PID
fi
}
status() {
if isrunning; then
echoGreen "Vert.x Application is running"
else
echoRed "Vert.x Application is either stopped or inaccessible"
fi
}
case "$1" in
start)
start
;;
status)
status
exit 0
;;
stop)
if isrunning; then
stop
exit 0
else
echoRed "Application not running"
exit 3
fi
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment