Last active
February 16, 2018 11:43
-
-
Save cinhtau/ace4276cb84a1a6f2a25ca39cac76413 to your computer and use it in GitHub Desktop.
Bash script for RiskShield Backoffice Component, use it as template 😉 License=https://choosealicense.com/licenses/mit/
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
#!/usr/bin/env bash | |
# --------------------------------------------------------------------------------------------- | |
# Batch file to start the RiskShield Back Office Component (BOC) | |
# ---------------------------------------------------------------------------------------------- | |
export LANG="en_US" | |
STAGE="test" | |
CHANNEL="issuing" | |
COLOR_SUCCESS="\\033[1;32m" | |
COLOR_FAILURE="\\033[1;31m" | |
COLOR_WARNING="\\033[1;33m" | |
COLOR_NORMAL="\\033[0;39m" | |
HEADER=$(printf '=%.0s' {1..60}) | |
name="RiskShield Back Office Component" | |
tan="boc" | |
SCRIPT_HOME="/opt/RiskShield/$CHANNEL/$STAGE/BOC" | |
# place pid according to FHS in /var/run is not possible, | |
# since it is not started as root | |
pidfile="$SCRIPT_HOME/boc.pid" | |
# setup env | |
JAVA_HOME=/usr/bin/java | |
BOC_RES="/opt/RiskShield/Resources/RS-BOC/current/lib" | |
# oracle jdbc driver | |
jdbc="/opt/RiskShield/Resources/JDBC/ojdbc6.jar" | |
CLASSPATH="$CLASSPATH:$jdbc" | |
for n in $BOC_RES/*.jar; do | |
CLASSPATH="$CLASSPATH:$n"; | |
done | |
status() { | |
if [ -f "$pidfile" ]; then | |
pid=`cat "$pidfile"` | |
if [ -e "/proc/$pid" ] ; then | |
# process by this pid is running. | |
# It may not be our pid, but that's what you get with just pidfiles. | |
return 0 | |
else | |
return 2 # program is dead but pid file exists | |
fi | |
else | |
return 3 # program is not running | |
fi | |
} | |
stop() { | |
# Try a few times to kill TERM the program | |
if status; then | |
pid=`cat "$pidfile"` | |
echo "Killing $name (pid $pid) with SIGTERM" | |
kill $pid | |
# Wait for it to exit. | |
for i in `seq 1 5`; do | |
echo "Waiting $name (pid $pid) to die..." | |
status || break | |
sleep 1 | |
done | |
if status ; then | |
echo "$name stop failed; still running." | |
else | |
echo "$name stopped." | |
rm $pidfile | |
fi | |
fi | |
} | |
force_stop() { | |
if status; then | |
stop | |
status && kill -9 `cat "$pidfile"` | |
rm $pidfile | |
fi | |
} | |
start() { | |
echo -e "Start $name using classpath $CLASSPATH \n" | |
cd $SCRIPT_HOME | |
${JAVA_HOME} \ | |
-Djava.library.path=bin \ | |
-classpath $CLASSPATH \ | |
com.riskshield.investigator.system.Starter RSI_Log4j.xml > /dev/null 2> "${SCRIPT_HOME}/$tan.err" & | |
# Generate the pidfile from here. If we instead made the forked process | |
# generate it there will be a race condition between the pidfile writing | |
# and a process possibly asking for status. | |
echo $! > $pidfile | |
} | |
debug_start() { | |
echo -e "Start $name using classpath $CLASSPATH \n" | |
cd $SCRIPT_HOME | |
${JAVA_HOME} -Djava.library.path=bin \ | |
-classpath $CLASSPATH \ | |
com.riskshield.investigator.system.Starter RSI_Log4j.xml | |
} | |
echo_success() { | |
echo -n -e $"[$COLOR_SUCCESS OK $COLOR_NORMAL]" | |
} | |
echo_stopped() { | |
echo -n -e $"[$COLOR_FAILURE STOP $COLOR_NORMAL]" | |
} | |
case "$1" in | |
debug) | |
debug_start | |
;; | |
start) | |
if [ -e $pidfile ] ; then | |
pid=$(cat $pidfile) | |
echo -n -e "Backoffice Component is already running under $COLOR_WARNING $pid $COLOR_NORMAL \n" | |
else | |
start | |
fi | |
;; | |
force-start) | |
status | |
code=$? | |
if [ $code -eq 0 ]; then | |
echo_success && echo -e "$name is $COLOR_SUCCESS running $COLOR_NORMAL, process `cat $pidfile`" | |
else | |
echo_stopped && echo -e "$name is not running" | |
if [ -f $pidfile ] | |
then rm $pidfile | |
fi | |
fi | |
start | |
code=$? | |
exit $code | |
;; | |
stop) | |
stop ;; | |
force-stop) force_stop ;; | |
status) | |
status | |
code=$? | |
if [ $code -eq 0 ]; then | |
echo -en "$HEADER\n" | |
echo_success && echo -e " $name is $COLOR_SUCCESS running $COLOR_NORMAL, process `cat $pidfile`" | |
echo -en "$HEADER\n" | |
else | |
echo -en "$HEADER\n" | |
echo_stopped && echo -e " $name is $COLOR_WARNING not running $COLOR_NORMAL" | |
echo -en "$HEADER\n" | |
fi | |
exit $code | |
;; | |
restart) | |
stop && start | |
;; | |
*) | |
echo "Usage: boc.sh {start|stop|force-start|force-stop|status|restart|debug}" | |
exit 3 | |
;; | |
esac | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment