Skip to content

Instantly share code, notes, and snippets.

@cnaude
Last active December 24, 2015 04:29
Show Gist options
  • Save cnaude/6744744 to your computer and use it in GitHub Desktop.
Save cnaude/6744744 to your computer and use it in GitHub Desktop.
Red Hat startup script for Minecraft
#!/usr/bin/perl
use strict;
my $date = localtime(time);
my $log_file = "/home/mc/tekkit/server.log";
my $re = '^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d \[SEVERE\] The server has stopped responding.*?';
print "[$date] Watching $log_file\n";
print "[$date] RE: $re\n";
open (TAIL, "tail -F $log_file|");
while (<TAIL>) {
if (/$re/) {
print "[$date] Server stopped responding\n";
system ("/home/mc/tekkit/rc-scripts/minecraft msg \"The server has stopped responding! Restarting...\"");
system ("/home/mc/tekkit/rc-scripts/minecraft restart");
}
}
close TAIL;
#!/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='Tekkit.jar'
USERNAME='mc'
MCPATH='/games/tekkit'
RUNFILE="${MCPATH}/minecraft.run"
INVOCATION="sh minecraft.sh"
BACKUPDIR=/backups/tekkit
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 tekkit
echo "Starting $SERVICE..."
cd $MCPATH
as_user "cd $MCPATH && screen -dmS tekkit $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 tekkit -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 tekkit -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S tekkit -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S tekkit -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 tekkit -X eval 'stuff \"say SERVER RESTARTING IN 10 SECONDS. Saving map...\"\015'"
mc_save
sleep 10
as_user "screen -p 0 -S tekkit -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 tekkit -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 tekkit -X eval 'stuff \"save-off\"\015'"
echo "Sending save-all command to $SERVICE"
as_user "screen -p 0 -S tekkit -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 tekkit -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
#!/bin/sh
export HEAPSIZE=10G
export JAVA_HOME=/usr/java/latest
export JAVA=$JAVA_HOME/bin/java
export PATH=$PATH:$JAVA_HOME/bin
export JARFILE="Tekkit.jar"
export LARGEPAGES="-XX:+UseLargePages -XX:LargePageSizeInBytes=2m"
touch minecraft.run
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
while [ -e minecraft.run ]
do
echo "[`date`] Starting Minecraft" | tee -a mc-startup.log
#$JAVA -server ${LARGEPAGES} ${GCOPT} -jar ${JARFILE} n
$JAVA ${LARGEPAGES} -d64 -XX:+UseCompressedOops -server -XX:+UseAdaptiveGCBoundary \
${LARGEPAGES} \
-Xmx${HEAPSIZE} -Xms${HEAPSIZE} \
-XX:TargetSurvivorRatio=90 \
-XX:SurvivorRatio=1 \
-XX:CompileThreshold=300 \
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC \
-XX:CMSInitiatingOccupancyFraction=80 \
-XX:SoftRefLRUPolicyMSPerMB=0 \
-XX:+UseParNewGC \
-XX:MaxHeapFreeRatio=25 \
-XX:MinHeapFreeRatio=21 \
-XX:+DisableExplicitGC \
-XX:MaxTenuringThreshold=4 \
-jar ${JARFILE} nogui
echo ""
echo "Press control-c within 5 seconds to exit."
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment