Skip to content

Instantly share code, notes, and snippets.

@cnaude
Created November 20, 2016 01:03
Show Gist options
  • Save cnaude/6e3a55761cfa3c612eb045437b38c121 to your computer and use it in GitHub Desktop.
Save cnaude/6e3a55761cfa3c612eb045437b38c121 to your computer and use it in GitHub Desktop.
minecraft rc script
#!/bin/bash
#
# $Id: minecraft,v 1.9 2011/10/04 04:29:02 cnaude Exp $
#
# /etc/rc.d/minecraft
# PROVIDE: minecraft
# REQUIRE: LOGIN
#Settings
SERVICE='AardMC.jar'
USERNAME='mc'
MCPATH='/games/aardmc'
RUNFILE="${MCPATH}/minecraft.run"
INVOCATION="sh minecraft.sh"
BACKUPDIR=/backups/aardmc
SVRMSG=$2
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
mc_start() {
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
screen -wipe sky
echo "Starting $SERVICE..."
cd $MCPATH
as_user "cd $MCPATH && screen -dmS mc $INVOCATION"
sleep 7
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
mc_msg() {
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
if [ "x$SVRMSG" != "x" ]; then
as_user "screen -p 0 -S mc -X eval 'stuff \"say ${SVRMSG}\"\015'"
else
echo "Usage: $0 msg \"Server message\""
fi
else
echo "Minecraft server is not running..."
fi
}
mc_stop() {
if [ -e $RUNFILE ]
then
echo "Removing $RUNFILE"
rm $RUNFILE
fi
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S mc -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S mc -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
mc_restart() {
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Restarting $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"say SERVER RESTARTING IN 10 SECONDS. Saving map...\"\015'"
mc_save
sleep 10
as_user "screen -p 0 -S mc -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running. Starting it up."
mc_start
fi
}
mc_save() {
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Sending save-all command to $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"save-all\"\015'"
else
echo "$SERVICE is not running."
fi
}
mc_backup() {
BU="world-`date +%Y%m%d.%H%M%S`.tar.gz"
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Sending save-off command to $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"save-off\"\015'"
echo "Sending save-all command to $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"save-all\"\015'"
echo "Waiting 5 seconds before attempting tar"
sleep 5
cd $MCPATH
echo "Creating $BU"
tar czvf $BACKUPDIR/$BU world
echo "Sending save-on command to $SERVICE"
as_user "screen -p 0 -S mc -X eval 'stuff \"save-on\"\015'"
else
echo "$SERVICE is not running."
fi
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
save)
mc_save
;;
backup)
mc_backup
;;
restart)
mc_restart
;;
msg)
mc_msg
;;
status)
if ps awwx | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: /etc/rc.d/minecraft {start|stop|status|restart|save|backup|msg}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment