Add multi-instance support to init scripts

This commit is contained in:
Ben Peddell 2015-11-22 19:40:25 +10:00
parent 92acb88fd5
commit 237fff6bc4
3 changed files with 160 additions and 60 deletions

View File

@ -20,46 +20,99 @@ NAME="ShooterGameServer"
LOGFILE="${logdir}/${NAME}.log" LOGFILE="${logdir}/${NAME}.log"
DAEMON="/usr/bin/arkmanager" DAEMON="/usr/bin/arkmanager"
SVCNAME="${0##*/}"
INSTANCE="${SVCNAME#*.}"
if [ "$INSTANCE" == "$SVCNAME" ]; then
INSTANCE="$2"
fi
set -e set -e
# If the daemon is not there, then exit. # If the daemon is not there, then exit.
test -x $DAEMON || exit 5 test -x $DAEMON || exit 5
function start_instance(){
local INSTANCE="$1"
PID="$(<"/var/run/arkmanager.${INSTANCE}.pid")"
if [ -n "$PID" ] && kill -0 "$PID" >/dev/null 2>&1; then
if grep " ${PID} .* ${DAEMON}" <(ps -ef) >/dev/null 2>&1; then
echo "$NAME @${INSTANCE} is already running"
return 0
fi
fi
log_daemon_msg "Starting" "$NAME @${INSTANCE}"
ulimit -n 100000
"${DAEMON}" run "@${INSTANCE}" &
PID="$!"
sleep 5
if kill -0 "$PID" >/dev/null 2>&1; then
echo "$PID" >"/var/run/arkmanager.${INSTANCE}.pid"
log_end_msg 0
return 0
else
log_end_msg 1
return 1
fi
}
function start_all_instances(){
local nosuccess=0
local anyfailure=0
for instance in $("${DAEMON}" list-instances --brief); do
if start_instance "$instance"; then
nosuccess=0
else
anyfailure=1
fi
done
return $nosuccess
}
function stop_instance(){
local INSTANCE="$1"
log_daemon_msg "Stopping $NAME @${INSTANCE}: "
"${DAEMON}" stop "@${INSTANCE}" &
rm -f "/var/run/arkmanager.${INSTANCE}.pid"
log_end_msg 0
return 0
}
case "$1" in case "$1" in
start) start)
log_daemon_msg "Starting" "$NAME" if [ -n "$INSTANCE" ]; then
ulimit -n 100000 start_instance "$INSTANCE"
su -s /bin/sh -c "$DAEMON start --all" $steamcmd_user exit $?
sleep 5
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "$PID" >/var/run/arkmanager.pid
log_end_msg 0
else else
log_end_msg 1 if start_all_instances; then
exit 0
else
exit 1
fi
fi fi
;; ;;
stop) stop)
log_daemon_msg "Stopping" "$NAME" if [ -n "$INSTANCE" ]; then
su -s /bin/sh -c "$DAEMON stop --all" $steamcmd_user stop_instance "$INSTANCE"
sleep 5 exit $?
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
log_end_msg 1
else else
rm /var/run/arkmanager.pid for instance in $("${DAEMON}" list-instances --brief); do
log_end_msg 0 stop_instance "$instance"
done
exit $?
fi fi
;; ;;
restart) restart)
ulimit -n 100000 "$0" stop
su -s /bin/sh -c "$DAEMON restart --all" $steamcmd_user "$0" start
;; ;;
status) status)
su -s /bin/sh -c "$DAEMON status" $steamcmd_user "$DAEMON" status "@${INSTANCE:-all}"
;; ;;
*) *)

View File

@ -13,27 +13,25 @@ depend(){
} }
start(){ start(){
INSTANCE="${RC_SVCNAME#*.}"
ebegin "Starting ARK manager daemon" ebegin "Starting ARK manager daemon"
ulimit -n 100000 ulimit -n 100000
su -s /bin/sh -c "$DAEMON start --all" $steamcmd_user if [ "$INSTANCE" != "$RC_SVCNAME" ]; then
sleep 5 "$DAEMON" start "@${INSTANCE}"
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
eend 0
else else
eend 1 "$DAEMON" start "@all"
fi fi
eend $?
} }
stop(){ stop(){
INSTANCE="${RC_SVCNAME#*.}"
ebegin "Stopping ARK manager daemon" ebegin "Stopping ARK manager daemon"
su -s /bin/sh -c "$DAEMON stop --all" $steamcmd_user if [ "$INSTANCE" != "$RC_SVCNAME" ]; then
sleep 5 "$DAEMON" stop "@${INSTANCE}"
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
eend 0
else else
eend 1 "$DAEMON" stop "@all"
fi fi
eend $?
} }

View File

@ -34,54 +34,103 @@ GREEN="\\033[1;32m"
RED="\\033[1;31m" RED="\\033[1;31m"
NORMAL="\\033[0;39m" NORMAL="\\033[0;39m"
SVCNAME="${0##*/}"
INSTANCE="${SVCNAME#*.}"
if [ "$INSTANCE" == "$SVCNAME" ]; then
INSTANCE="$2"
fi
set -e set -e
# If the daemon is not there, then exit. # If the daemon is not there, then exit.
test -x $DAEMON || exit 5 test -x $DAEMON || exit 5
function start_instance(){
local INSTANCE="$1"
PID="$(<"/var/run/arkmanager.${INSTANCE}.pid")"
if [ -n "$PID" ] && kill -0 "$PID" >/dev/null 2>&1; then
if grep " ${PID} .* ${DAEMON}" <(ps -ef) >/dev/null 2>&1; then
echo "$NAME @${INSTANCE} is already running"
return 0
fi
fi
echo -n "Starting $NAME @${INSTANCE}: "
ulimit -n 100000
"${DAEMON}" run "@${INSTANCE}" &
PID="$!"
sleep 5
if kill -0 "$PID" >/dev/null 2>&1; then
echo "$PID" >"/var/run/arkmanager.${INSTANCE}.pid"
touch "/var/lock/subsys/arkmanager.${INSTANCE}"
echo "[" "$GREEN" " OK " "$NORMAL" "]"
return 0
else
echo "[" "$RED" " FAILED " "$NORMAL" "]"
return 1
fi
}
function start_all_instances(){
local nosuccess=0
local anyfailure=0
for instance in $("${DAEMON}" list-instances --brief); do
if start_instance "$instance"; then
nosuccess=0
else
anyfailure=1
fi
done
return $nosuccess
}
function stop_instance(){
local INSTANCE="$1"
echo -n "Stopping $NAME @${INSTANCE}: "
"${DAEMON}" stop "@${INSTANCE}" &
rm -f "/var/lock/subsys/arkmanager.${INSTANCE}"
rm -f "/var/run/arkmanager.${INSTANCE}.pid"
echo "[" "$GREEN" " OK " "$NORMAL" "]"
return 0
}
case "$1" in case "$1" in
start) start)
echo -n "Starting $NAME: " if [ -n "$INSTANCE" ]; then
ulimit -n 100000 start_instance "$INSTANCE"
su -s /bin/sh -c "$DAEMON start --all" $steamcmd_user > /dev/null exit $?
sleep 5
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "${PID}" >/var/run/arkmanager.pid
touch /var/lock/subsys/arkmanager
echo "[" "$GREEN" " OK " "$NORMAL" "]"
exit 0
else else
echo "[" "$RED" " FAILED " "$NORMAL" "]" if start_all_instances; then
exit 1 touch /var/lock/subsys/arkmanager
exit 0
else
exit 1
fi
fi fi
;; ;;
stop) stop)
echo -n "Stopping $NAME: " if [ -n "$INSTANCE" ]; then
su -s /bin/sh -c "$DAEMON stop --all" $steamcmd_user > /dev/null stop_instance "$INSTANCE"
sleep 5 exit $?
PID=`ps -ef | grep $NAME | grep -v grep | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "[" "$RED" " FAILED " "$NORMAL" "]"
exit 1
else else
echo "[" "$GREEN" " OK " "$NORMAL" "]" for instance in $("${DAEMON}" list-instances --brief); do
rm -f /var/lock/subsys/arkmanager stop_instance "$instance"
rm -f /var/run/arkmanager.pid done
exit 0 rm -f "/var/lock/subsys/arkmanager"
exit $?
fi fi
;; ;;
restart) restart)
echo -n "Restarting $NAME: " "$0" stop
ulimit -n 100000 "$0" start
su -s /bin/sh -c "$DAEMON restart --all" $steamcmd_user > /dev/null
echo "OK"
;; ;;
status) status)
su -s /bin/sh -c "$DAEMON status" $steamcmd_user "$DAEMON" status "@${INSTANCE:-all}"
exit 0 exit 0
;; ;;