Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/etc/init.d/minecraft script to handle minecraft auto-start and backups...
5 */6 * * * /etc/init.d/minecraft backup #backup four times a day, meaning every 6 hours
15 */2 * * * /etc/init.d/minecraft map #generate map every two hours
10 1 * * * /etc/init.d/minecraft clean #clean old files daily
#!/bin/bash
# /etc/init.d/minecraft
# version 0.4.0.1 2012-03-03 (YYYY-MM-DD)
# Modified by Hauke Schade
# Modified by Mark Holmquist
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# 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: Minecraft server
# Description: Starts the minecraft server
### END INIT INFO
#Settings
SERVICE='craftbukkit.jar'
OPTIONS='nogui'
USERNAME='minecraft'
WORLD='world'
WORLD_NETHER='world_nether'
WORLD_END='world_the_end'
MCPATH="/opt/minecraft"
MOPATH="/opt/minecraft/dev/Minecraft-Overviewer"
MOCONFIG="/opt/minecraft/dev/Minecraft-Overviewer/config.py"
BACKUPPATH="/media/backup/minecraft.backup"
CPU_COUNT=1
INVOCATION="java -Xmx1024M -Xms1024M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar $SERVICE $OPTIONS"
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
mc_start() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $MCPATH
as_user "screen -dmS minecraft $INVOCATION"
x=1
while [ $x -le 7 ]
do
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is now running."
break
fi
x=$(( $x + 1 ))
sleep 1
done
if [ "$x" -ge 8 ]
then
echo "Error! Could not start $SERVICE!"
fi
fi
}
mc_saveoff() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... suspending saves"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
fi
}
mc_saveon() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... re-enabling saves"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"
fi
}
mc_stop() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'"
else
echo "$SERVICE was not running."
fi
x=1
while [ "$x" -le 7 ]
do
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
x=$(( $x + 1 ))
sleep 1
else
echo "$SERVICE is stopped."
break
fi
done
if [ "$x" -ge 8 ]
then
echo "Error! $SERVICE could not be stopped."
fi
}
mc_backup() {
echo "Backing up minecraft"
timestamp=`date +%Y%m%d-%H%M`
if [ -f "$BACKUPPATH/minecraft_${timestamp}.tar.gz" ]
then
echo "blub, backup already existed"
else
cd $MCPATH
as_user "tar -czhf $BACKUPPATH/minecraft_${timestamp}.tar.gz $SERVICE $WORLD $WORLD_NETHER $WORLD_END server.log"
fi
echo "Backup complete to $BACKUPPATH/minecraft_${timestamp}.tar.gz"
}
mc_map() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep "python overviewer.py" > /dev/null
then
echo "Already doing mapping process"
else
echo "beginning mapping for overviewer"
DATE=`date +%Y%m%d%H%M`
mc_saveoff
as_user "cp -rp $MCPATH/$WORLD /tmp/$WORLD.$DATE"
mc_saveon
as_user "cd $MOPATH && nice python overviewer.py --config=$MOCONFIG"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say we have a new map!\"\015'"
as_user "rm -rf /tmp/$WORLD.$DATE"
echo "done with mapping"
fi
}
mc_cleanold() {
echo "Cleaning Old Files"
find $BACKUPPATH -type f -mtime +24 -exec rm -f -v {} \;
echo "Done, cleaning up files"
}
mc_command() {
if [ "$1" ]
then
command="$1";
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S minecraft -X eval 'stuff \"$command\"\015'"
fi
else
echo "Must specify server command"
fi
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
backup)
mc_saveoff
mc_backup
mc_saveon
;;
map)
mc_map
;;
clean)
mc_cleanold
;;
status)
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
command)
mc_command "$2"
;;
*)
echo "Usage: /etc/init.d/minecraft {start|stop|backup|clean|status|restart|map|command \"server command\"}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment