Skip to content

Instantly share code, notes, and snippets.

@ranaroussi
Forked from aidoom/ib-controller-init
Last active April 24, 2016 10:07
Show Gist options
  • Save ranaroussi/ca2a1d7b1312e6906648 to your computer and use it in GitHub Desktop.
Save ranaroussi/ca2a1d7b1312e6906648 to your computer and use it in GitHub Desktop.
IBContoller init script
#! /bin/sh
### BEGIN INIT INFO
# Provides: IBController
# Short-Description: starts instance of IBController
# Description: starts instance of IBController using start-stop-daemon
### END INIT INFO
# IBController
IBC_PATH="/path/to/IBController"
IBC_INI="/path/to/IBController/IBController.ini"
IBC_JAR=IBController.jar
# TWS/GW
TWS_PATH="/path/to/IBTWS-OR-IBGW"
TWS_JARS="${TWS_PATH}/jts.jar:${TWS_PATH}/total.2013.jar"
# JAVA/Daemon
JAVAOPTS='-Xmx768M -XX:MaxPermSize=256M'
DAEMON_OPTS=" -a java -cp ${TWS_JARS}:${IBC_PATH}/${IBC_JAR} ${JAVAOPTS} ibcontroller.IBGatewayController ${IBC_INI}"
##########################################
# user
RUN_AS=root
# path to xvfb bin
DAEMON=/usr/bin/xvfb-run
# Path to store PID file
PID_FILE="/var/run/ibcontroller/xvfb.pid"
PID_PATH=$(dirname $PID_FILE)
# app/script name
NAME=IBController
# startup args
############# END EDIT ME ################
get_pid() {
cat "$PID_FILE"
}
is_running() {
[ -f "$PID_FILE" ] && ps `get_pid` > /dev/null 2>&1
}
##########################################
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
if is_running; then
echo "Already started (pid" `get_pid`")"
else
echo "Starting $NAME..."
rm -rf $PID_PATH || return 1
install -d --mode=0755 -o $RUN_AS $PID_PATH || return 1
start-stop-daemon -d $IBC_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
echo " * $NAME started successfully (pid" `get_pid`")"
if ! is_running; then
echo "Unable to start $NAME"
exit 1
fi
fi
;;
stop)
if ! is_running; then
echo " * $NAME isn't running"
exit 1
else
echo " * Stopping $NAME..."
# kill `get_pid`
# hacky kill command because we can't get PID of java process when starting daemon
pgrep -f "(${IBC_JAR}.*.ini)" | xargs kill -9
for i in {1..10}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
if is_running; then
echo " * $NAME Not stopped; may still be shutting down or shutdown may have failed."
exit 1
else
echo " * $NAME stopped successfully"
if [ -f "$PID_FILE" ]; then
rm -f $PID_FILE
fi
fi
fi
;;
restart)
$0 stop
if is_running; then
echo " * Unable to stop $NAME. Will not attempt to start."
exit 1
fi
$0 start
;;
status)
if is_running; then
echo " * $NAME is running (pid" `get_pid`")"
else
echo " * $NAME is not running"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment