Skip to content

Instantly share code, notes, and snippets.

@DmitryRendov
Last active August 12, 2018 07:57
Show Gist options
  • Save DmitryRendov/ced9330728525513673aedbb568ac3e9 to your computer and use it in GitHub Desktop.
Save DmitryRendov/ced9330728525513673aedbb568ac3e9 to your computer and use it in GitHub Desktop.
Starbound Linux server (screen)
#!/bin/bash -e
### BEGIN INIT INFO
# Provides: starbound
# Required-Start: $local_fs $remote_fs screen-cleanup
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starbound server
# Description: Starts the starbound server
### END INIT INFO
# Settings
SERVICE='starbound_server'
USERNAME='starbound'
SCREENNAME='starbound'
HISTORY=1000
SBPATH='/home/starbound/GOGGames/Starbound/game/linux'
SBWORLD='/home/starbound/GOGGames/Starbound/game'
BACKUPPATH='/home/starbound/backups/starbound'
D2BACKUP[0]='storage'
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
sb_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $SBPATH
as_user "cd $SBPATH && screen -h $HISTORY -dmS $SCREENNAME ./$SERVICE"
sleep 7
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
sb_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
pkill -SIGINT -u $USERNAME -f $SERVICE > /dev/null
sleep 7
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
sb_backup() {
umask 077
NOW=`date "+%Y-%m-%d_%Hh%M"`
BACKUP_FILE="$BACKUPPATH/${NOW}.tar"
echo ""
echo -n "Removing old backcopies..."
echo ""
find $BACKUPPATH/*.gz -mtime +14 -exec rm {} \;
echo ""
echo "Backing up Starbound Universe..."
echo ""
for object in ${D2BACKUP[*]}
do
echo ""
echo -n "Backuping ${object}"
as_user "tar -C \"$SBWORLD\" -rf \"$BACKUP_FILE\" $object"
done
echo ""
echo "Compressing backup..."
as_user "gzip -f \"$BACKUP_FILE\""
chmod 0600 "${BACKUP_FILE}.gz"
echo "Done."
}
sb_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
pre_log_len=`wc -l "$SBWORLD/starbound_server.log" | awk '{print $1}'`
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S $SCREENNAME -X eval 'stuff \"$command\"\015'"
sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $[`wc -l "$SBWORLD/starbound_server.log" | awk '{print $1}'`-$pre_log_len] "$SBWORLD/starbound_server.log"
fi
}
# The main events processor
case "$1" in
start)
sb_start
;;
stop)
sb_stop
;;
restart)
sb_stop
sb_start
;;
backup)
sb_backup
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
command)
if [ $# -gt 1 ]; then
shift
sb_command "$*"
else
echo "Must specify server command (try 'help'?)"
fi
;;
*)
echo "Usage: $0 {start|stop|backup|status|restart|command \"server command\"}"
exit 1
;;
esac
exit 0
@DmitryRendov
Copy link
Author

My personal Starbound Linux server runner to launch game server in Screen session.

Supports:

  • start/stop/restart Starbound linux server;
  • add to server startup - copy the script to /etc/init.d/ and make update-rc.d bungeecord defaults;
  • backup server and remove 14+ days old copies;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment