#!/bin/bash
### BEGIN INIT INFO
# Provides: B4J Server module
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop B4J Server module
### END INIT INFO
# chkconfig: 35 92 1
#customizable options:
#module name
JAR_NAME=B4J.jar
#installation directory
JAR_DIR=/home/ec2-user/B4J
#no need to customize anything below this line
RUNAS_USER=ec2-user
B4J_START="cd ${JAR_DIR} ; nohup java -jar ${JAR_NAME} >nohup.out 2>&1 &"
RUN_USER=$(whoami)
b4j_pid() {
echo $(ps -f -C java | grep ${JAR_NAME}$ | grep -v grep | awk '{ print $2 }')
}
start() {
pid=$(b4j_pid)
if [ -n "$pid" ]; then
echo "Already running with pid: $pid"
else
echo "Starting..."
if [ "$RUN_USER" == "$RUNAS_USER" ]; then
echo "Running with current user"
bash -c "$B4J_START"
else
echo "Running with switch user"
su - $RUNAS_USER -c "$B4J_START"
fi
status
fi
}
stop() {
pid=$(b4j_pid)
if [ -n "$pid" ]; then
echo "Stopping..."
kill $pid
else
echo "Server not running"
fi
}
status() {
pid=$(b4j_pid)
if [ -n "$pid" ]; then
echo "Running with pid: $pid"
else
echo "Not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0