Skip to content

Instantly share code, notes, and snippets.

@thangdc94
Last active December 3, 2019 02:01
Show Gist options
  • Save thangdc94/d7ae1391192e8c28c141a4203ced3db5 to your computer and use it in GitHub Desktop.
Save thangdc94/d7ae1391192e8c28c141a4203ced3db5 to your computer and use it in GitHub Desktop.
Deploy service
#!/usr/bin/env bash
waitForProcessEnd() {
pidKilled=$1
processedAt=`date +%s`
while kill -0 ${pidKilled} > /dev/null 2>&1;
do
echo -n "."
sleep 1;
if [ $(( `date +%s` - $processedAt )) -gt 60 ]; then
break;
fi
done
# process still there : kill -9
if kill -0 ${pidKilled} > /dev/null 2>&1; then
kill -9 ${pidKilled} > /dev/null 2>&1
fi
# Add a CR after we're done w/ dots.
echo
}
usage="Usage: $0 (start|stop|status) <args...>"
if [ $# -le 0 ]; then
echo ${usage}
exit 1
fi
command=$1
shift
this="${BASH_SOURCE-$0}"
while [ -h "$this" ]; do
ls=`ls -ld "$this"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
this="$link"
else
this=`dirname "$this"`/"$link"
fi
done
# convert relative path to absolute path
bin=`dirname "$this"`
script=`basename "$this"`
bin=`cd "$bin">/dev/null; pwd`
this="$bin/$script"
if [ -z "$APP_HOME" ]; then
export APP_HOME=`dirname "$this"`/..
fi
APP_CONF_DIR="${APP_CONF_DIR:-$APP_HOME/conf}"
if [ -f "$APP_CONF_DIR/env.sh" ]; then
. "$APP_CONF_DIR/env.sh"
fi
if [ -z ${APP_MAIN_CLASS} ]; then
echo "Use must set app main class name in conf/env.sh"
exit 1
fi
if [ "$APP_LOG_DIR" = "" ]; then
APP_LOG_DIR="$APP_HOME/logs"
fi
mkdir -p ${APP_LOG_DIR}
logout=${APP_LOG_DIR}/app.out
if [ "$APP_PID_DIR" = "" ]; then
APP_PID_DIR="/tmp"
fi
pid=${APP_PID_DIR}/${APP_MAIN_CLASS}.pid
case ${command} in
(start)
mkdir -p "$APP_PID_DIR"
if [ -f ${pid} ]; then
if kill -0 `cat ${pid}` > /dev/null 2>&1; then
echo App running as process `cat ${pid}`. Stop it first.
exit 1
fi
fi
nohup ${bin}/start.sh $@ > /dev/null 2>&1 &
echo $! > ${pid}
echo "App started"
;;
(stop)
if [ -f ${pid} ]; then
pidToKill=`cat ${pid}`
if kill -0 ${pidToKill} > /dev/null 2>&1; then
echo -n "stopping app"
kill ${pidToKill} > /dev/null 2>&1
waitForProcessEnd ${pidToKill}
else
retval=$?
echo no app to stop because kill -0 of pid ${pidToKill} failed with status ${retval}
fi
else
echo no app to stop because no pid file ${pid}
fi
rm -f ${pid}
;;
(status)
if [ -f ${pid} ]; then
echo PID file at ${pid}
process_id=`cat ${pid}`
if [ -n "$(ps -p ${process_id} -o pid=)" ]; then
echo App running as process ${process_id}.
else
echo App with process ${process_id} not running
fi
else
echo No pid file ${pid}
fi
;;
esac
#!/usr/bin/env bash
# Java to use
# export JAVA_HOME=/usr
# Main class name, must be set
export APP_MAIN_CLASS=vn.com.viettel.prism.fbbot.scheduler.App
# App name, will appear in jps
# export APP_NAME=
# Heap size
# export APP_HEAPSIZE=1000
# Extra class path
# export APP_CLASSPATH=
# Extra runtime opt
CONTAINER_MEMORY_OPT="-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap"
DEBUG_OPT="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
export APP_OPTS="-XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError"
# Log
# export APP_LOG_DIR=
# export APP_LOGFILE=
export APP_ROOT_LOGGER=INFO,DRFA
# Daemonize
# export APP_PID_DIR=/tmp
# Set run mode
if [ -z ${RUNMODE} ]; then
export RUNMODE="dev"
fi
#!/usr/bin/env bash
this="${BASH_SOURCE-$0}"
while [ -h "$this" ]; do
ls=`ls -ld "$this"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '.*/.*' > /dev/null; then
this="$link"
else
this=`dirname "$this"`/"$link"
fi
done
# convert relative path to absolute path
bin=`dirname "$this"`
script=`basename "$this"`
bin=`cd "$bin">/dev/null; pwd`
this="$bin/$script"
if [ -z "$APP_HOME" ]; then
export APP_HOME=`dirname "$this"`/..
fi
APP_CONF_DIR="${APP_CONF_DIR:-$APP_HOME/conf}"
if [ -f "$APP_CONF_DIR/env.sh" ]; then
. "$APP_CONF_DIR/env.sh"
fi
if type -p java; then
echo Found java executable in PATH
JAVA="java"
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
echo Found java executable in JAVA_HOME
JAVA=${JAVA_HOME}/bin/java
else
cat 1>&2 <<EOF
+======================================================================+
| Error: JAVA_HOME is not set and Java could not be found |
+----------------------------------------------------------------------+
| Please download the latest Sun JDK from the Sun Java web site |
| > http://java.sun.com/javase/downloads/ < |
| |
| This app requires Java 1.6 or later. |
| NOTE: This script will find Sun Java whether you install using the |
| binary or the RPM based installer. |
+======================================================================+
EOF
exit 1
fi
JAVA_HEAP_MAX=-Xmx100m
if [ "$APP_HEAPSIZE" != "" ]; then
SUFFIX="m"
if [ "${APP_HEAPSIZE: -1}" == "m" ] || [ "${APP_HEAPSIZE: -1}" == "M" ]; then
SUFFIX=""
fi
if [ "${APP_HEAPSIZE: -1}" == "g" ] || [ "${APP_HEAPSIZE: -1}" == "G" ]; then
SUFFIX=""
fi
JAVA_HEAP_MAX="-Xmx""$APP_HEAPSIZE""$SUFFIX"
fi
CLASSPATH="${APP_CONF_DIR}"
for f in ${APP_HOME}/dist/*.jar; do
CLASSPATH=${CLASSPATH}:${f};
done
for f in ${APP_HOME}/lib/*.jar; do
CLASSPATH=${CLASSPATH}:${f};
done
if [ "$APP_CLASSPATH" != "" ]; then
CLASSPATH=${CLASSPATH}:${APP_CLASSPATH}
fi
if [ "$APP_LOG_DIR" = "" ]; then
APP_LOG_DIR="$APP_HOME/logs"
fi
if [ "$APP_LOGFILE" = "" ]; then
APP_LOGFILE='app.log'
fi
if [ "$APP_ROOT_LOGGER" = "" ]; then
APP_ROOT_LOGGER='INFO,console'
fi
APP_OPTS="$APP_OPTS -Dapp.log.dir=$APP_LOG_DIR"
APP_OPTS="$APP_OPTS -Dapp.log.file=$APP_LOGFILE"
APP_OPTS="$APP_OPTS -Dapp.root.logger=${APP_ROOT_LOGGER:-INFO,console}"
if [ -z ${APP_MAIN_CLASS} ]; then
echo "You must set app main class name in conf/env.sh"
exit 1
fi
if [ ! -z ${APP_NAME} ]; then
APP_OPTS="$APP_OPTS -Dproc_$APP_NAME"
fi
export CLASSPATH
exec ${JAVA} -XX:OnOutOfMemoryError="kill -9 %p" ${JAVA_HEAP_MAX} ${APP_OPTS} ${APP_MAIN_CLASS} $@
|
|--bin
| |--daemon.sh
| |--start.sh
|--conf
| |--env.sh
| |--congig.properties
|-- dist
| |--main.jar
|--lib
|--logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment