Skip to content

Instantly share code, notes, and snippets.

@sgreiner
Last active August 29, 2015 14:12
Show Gist options
  • Save sgreiner/bce10c9e5865dc3c7085 to your computer and use it in GitHub Desktop.
Save sgreiner/bce10c9e5865dc3c7085 to your computer and use it in GitHub Desktop.
#!/bin/sh
### BEGIN INIT INFO
# Provides: starbound
# Required-Start: networking
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Starbound Server Daemon
# Description: Starts/Stops/Restarts the Starbound Server Daemon
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Starbound Server Daemon"
NAME=starbound
DIR=/home/starbound-server/Steam/steamapps/common/Starbound/linux64
DAEMON=$DIR/starbound_server
PIDDIR=/var/run/starbound
PIDFILE=$DIR/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
USER=starbound-server
GROUP=starbound-server
DAEMON_ARGS=""
STEAM_DIR=/home/starbound-server
STEAM_DAEMON=$STEAM_DIR/steamcmd.sh
STEAM_USER=anonymous
STEAM_PASS=
STEAM_APP="211820 -beta unstable"
[ -x "$DAEMON" ] || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
check_pid_folder() {
if [ ! -d $PIDDIR ]; then
mkdir -p $PIDDIR
chown -R $USER:$GROUP $PIDDIR
fi
}
do_start() {
check_pid_folder
if [ -e $PIDFILE ]; then
PID=`cat $PIDFILE`
if ( ps -p $PID > /dev/null ); then
log_failure_msg "$DESC '$NAME' is already running."
return 1
else
rm -f $PIDFILE
start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --exec $DAEMON --test > /dev/null || return 1
start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --exec $DAEMON -- $DAEMON_ARGS || return 2
fi
else
start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --exec $DAEMON --test > /dev/null || return 1
start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --exec $DAEMON -- $DAEMON_ARGS || return 2
fi
}
do_stop() {
if [ -e $PIDFILE ]; then
PID=`cat $PIDFILE`
if ( ps -p $PID > /dev/null ); then
start-stop-daemon --stop --signal 2 --quiet --pidfile $PIDFILE
[ "$?" = 2 ] && return 2
else
log_failure_msg "$DESC '$NAME' is not running."
rm -f $PIDFILE
return 1
fi
else
log_failure_msg "$DESC '$NAME' is not running."
return 1
fi
}
case "$1" in
start)
log_daemon_msg "Starting $DESC..." "$NAME"
do_start
case "$?" in
0|1) log_end_msg 0 ;;
1) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC..." "$NAME"
do_stop
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC..." "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
*) log_end_msg 1 ;;
esac
;;
*)
log_end_msg 1
;;
esac
;;
status)
if [ -e $PIDFILE ]; then
PID=`cat $PIDFILE`
if ( ps -p $PID > /dev/null ); then
log_success_msg "$DESC '$NAME' is running (pid $PID)."
exit 0
else
log_failure_msg "$DESC '$NAME' is not running."
rm -f $PIDFILE
exit 1
fi
else
log_failure_msg "$DESC '$NAME' is not running."
exit 1
fi
;;
update)
su - $USER -c "$STEAM_DAEMON +login $STEAM_USER $STEAM_PASS +app_update $STEAM_APP +quit"
;;
*)
log_action_msg "Usage: $SCRIPTNAME {start|stop|restart|status|update}"
exit 0
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment