Minecraft Server on CentOS 6

I downloaded the minecraft server package and extracted it to /opt/minecraft

I installed screen and Java (OpenJDK) using yum as follows:

yum install java-1.7.0-openjdk screen

The SysV init startup scripts provided on the Wiki didn’t work for me so I cobbled the following together which works for me. Anyone else wishing to use this should set the variables at the top correctly. Place this file in the /etc/init.d/ directory and make it executable
chmod +x /etc/init.d/minecraft
Then to stop and start minecraft use the following:
/etc/init.d/minecraft start
/etc/init.d/minecraft stop

It would also be good to ensure that minecraft starts automatically when the system boots:
chkconfig minecraft on
The SysV init script I used is shown below:

#!/bin/bash
#
# chkconfig: 35 90 12
# description: Minecraft Server
PATH_TO_JRE="/usr/java/jre1.7.0_13/bin"
PATH_TO_JRE_LIBS="/usr/java/jre1.7.0_13/lib/amd64/"
PATH_TO_SCREEN="/usr/bin/screen"
SERVICE_NAME="minecraft"
PATH_TO_MINECRAFT="/opt/minecraft"
MEMORY_SIZE="512M"
#
# Get function from functions library
. /etc/init.d/functions

# Check dependencies
if [ ! -f $PATH_TO_JRE/java ];
then
echo “Please configure this script by setting the path to a Java runtime.”
exit 1
fi
if [ ! -f $PATH_TO_SCREEN ];
then
echo “Please ensure screen is installed (yum install screen) and the path is set correctly at the top of this script.”
exit 1
fi
if [ ! -f $PATH_TO_MINECRAFT/minecraft_server.jar ];
then
echo “Please ensure the minecraft server jar file is in the correct location and configured at the top of this script.”
exit 1
fi

# Start the service
start() {
if [ -f /var/lock/subsys/$SERVICE_NAME ];
then
echo “Lock file $FILE exists. Is $SERVICE_NAME still running?”
exit 1
fi
echo -n $”Starting $SERVICE_NAME server: ”

export LD_LIBRARY_PATH=”$PATH_TO_JRE_LIBS”
cd $PATH_TO_MINECRAFT
$PATH_TO_SCREEN -h 1024 -dmS minescreen $PATH_TO_JRE/java -Xmx$MEMORY_SIZE -Xms$MEMORY_SIZE -jar $PATH_TO_MINECRAFT/minecraft_server.jar nogui

### Create the lock file ###
touch /var/lock/subsys/$SERVICE_NAME
success $”$SERVICE_NAME server startup”
echo
}
stop() {
echo -n $”Stopping $SERVICE_NAME server: ”

PIDS=`ps -ef | grep “minecraft” | grep -v “grep” | awk ‘{print $2}’ | head -1`
for PID in $PIDS
do
kill $PID
done

### Now, delete the lock file ###
rm -f /var/lock/subsys/$SERVICE_NAME
}
### main logic ###
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
status $SERVICE_NAME
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
exit 1
esac
exit 0

Backups

I added a cron job to backup the minecraft directory regularly using the following script. You will need to create the target directories first e.g.
mkdir -p /var/backups/minecraft/byday/{Sun,Mon,Tue,Wed,Thu,Fri,Sat}


#!/bin/bash
DAY=`date +%a`
/bin/nice /bin/tar czpf /var/backups/minecraft/byday/$DAY/minecraft.tar.gz --same-owner /opt/minecraft 2>/var/backups/minecraft_backup_error.log

Screen

Since we are using ‘screen’ to hold the session output we can use the following screen commands:
To view the current list of available screen sessions:
screen -ls
To re-attach to one of these screen sessions:
screen -r 12345
To detach again from the session (while leaving the minecraft server running):
Ctrl-A d

Whitelist

I wanted a private minecraft server for my son and a few of his friends so I added their usernames to the white-list.txt file. One username per line – it looks like the username or the email address will work. I then changed the server.properties file and set white-list=true

Operations

I added my sons username to the ops.txt file which should allow him to send server commands e.g. whitelisting other users or setting the difficulty. A list of the ops commands is available here:
http://www.minecraftwiki.net/wiki/SMP_Server_commands#Operator-only_commands

3 Responses to “Minecraft Server on CentOS 6”

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>