Owner’s Corner: Start Script Part 2
As I pointed out last week, we use a generic server-start script. That needed to go. It was built to accommodate a bunch of different configurations, but we use a very specific configuration. To that end, I've re-written the script, and commented it out like a good little coder should do.
The macro-level view of things that I changed:
- Removed all the multi-server stuff...we're only running 1 at a time.
- Remove all the auto-backup stuff for bukkit and bukkit plugins, we do all that manually. I don't like crap updating itself without some review.
- Added some command-line flags so we have server control with a few less steps
- Cleaned up all unnecessary menus and log messages.
So, without further ado, here's the new script:
#!/bin/bash
#
# Title: script.servermgmt.sh
# Description: Control a Minecraft Server
# Author: Tealk @ anzahcraft.de
# Modified By: Joseph Gullo (surfrock66) (surfrock66@surfrock66.com)
#
# This script controls all elements of the start, stop, restart,
# update and logging of a minecraft server. It utilizes screen
# to keep the process alive in a connectable interface in the background
#
# The script can optionally take an argument, one of the following:
# serverstart - Start the server
# serverstop - Stop the server
# serverrestart - Restart the server
# serverstatus - Print the serverstatus
#
#
# Start of Configuration
#
# Directory path the server runs from
DIR="/var/bukkit"
# Name to assign to the screen
NAME="Bukkit"
# Description to be used in log messages
DESC="teh3l3m3nts Minecraft Server"
# String to indicate the main world folder; default is "world"
WORLDNAME="world"
# Configurations for the actual server's java command
# JRE Command
DAEMON="java"
# Parameters to pass to the JRE
PARAMS="-server -Xmx3400M -jar"
# Flag to use bukkit or not; 1 = True, 0 = False
SERVERMOD="1"
# Actual filename for the bukkit archive
MODJAR="craftbukkit-1.3.2-R1.0.jar"
# Any trailing JRE flags
PARAMS2="nogui"
# Optional script to clean the logs when the server is gracefully shut down
LOGSCRUBBER=$DIR"/script.logcleaner.sh"
# Date and time string for use, ***DO NOT EDIT***
DATE=$(date +%d.%m.%y-%H.%M)
#
# End of configuration
#
# Function for starting the server properly
STARTS() {
# Check that there is not already a server running, by checking for an
# existing screen session of the same name.
if [[ `screen -ls |grep "$NAME"` ]]; then
echo "found running process: $DESC"
echo "Dont start the Server twice"
exit 1
# If the server isn't running, try to start it
else
# Check to see that the server isn't being started by root, which
# would be dramatically irresponsible. Don't allow processes to
# run as root when they accept user commands silly!
if [ "$UID" = "0" ]; then
echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
for c in $(seq 1 5); do
echo -n "!"
sleep 1
done
echo !
fi
# Start the server officially.
echo "starting the $DESC"
# Check that the directory path the serve is said to live in exists
if [ -e "$DIR" ];then
# If the directory exists, enter it.
if [ -x "$DIR" ]; then
cd "$DIR"
# Check to make sure the server files are executable
if [ ! -x $DATA ]; then
# If the files weren't executable, make them so!
# Picard-style
echo "$DATA is not executable, trying to set it"
chmod u+x $DATA
# Assuming the existing server files are executable...
else
# Check if bukkit is enabled in the config
if [ "$SERVERMOD" = "1" ]; then
echo "Starting... \" $NAME $DAEMON $PARAMS $MODJAR \""
echo "This may take a minute..."
screen -dmS $NAME $DAEMON $PARAMS $MODJAR
# If bukkit isn't enabled in the config, just run the plain server file.
else
# Initiate a screen, disconnecting any existing
# screens of the same name, using the java and
# server configs set in the master config.
screen -dmS $NAME $DAEMON $PARAMS minecraft_server.jar $PARAMS2
fi
fi
# If the directory doesn't exist, abort
else
echo "No such directory: $DIR!"
fi
fi
fi
}
# Function for stopping the server properly
STOP() {
if [[ `screen -ls |grep "$NAME"` ]]; then
echo -n "Stopping $DESC"
echo
screen -dr "$NAME" -p 0 -X stuff "$(printf "save-all\r")"
echo "30 Second Warning."
screen -dr "$NAME" -p 0 -X stuff "$(printf "say Server will restart in 30s !\r")"
sleep 20
echo "10 Second Warning."
screen -dr "$NAME" -p 0 -X stuff "$(printf "say Server will restart in 10s ! Please dissconnect \r")"
sleep 10
screen -dr "$NAME" -p 0 -X stuff "$(printf "stop\r")"
echo "Sopping Server wait 60 Seconds"
sleep 60
screen -r "$NAME" -p 0 -X quit
echo " done."
if [ -e $LOGSCRUBBER ]; then
sh $LOGSCRUBBER "$DIR"/server.log "$DIR"/full_server.log
else
cat "$DIR"/server.log >> "$DIR"/full_server.log
fi
rm -rf "$DIR"/server.log
else
echo "Coulnd't find a running $DESC"
fi
}
# Function to speak something to the server through the console
SAYS() {
# Check that a screen representing the server is active, implying
# that the server is running
if [[ `screen -ls |grep "$NAME"` ]]; then
# Pass user input into the screen representing the server using the
# screen command prefixing it with "say"
screen -S "$NAME" -p 0 -X stuff "$(printf "say $say1\r")"
sleep 1
# If no screen representing the server is active, fail with a message
else
echo "$DESC seems to be offline"
fi
}
# Function to connect to a running server
SCREENS() {
# Check to see that a screen with the desired name exists, as confirmation
# that the server is online
if [[ `screen -ls |grep "$NAME"` ]]; then
# Connect to the screen with the desired name
screen -r "$NAME"
else
# Indicate if the server looks to be offline.
echo "$DESC seems to be offline"
fi
}
# Function to just print the server status
STATUS() {
# Generate a list of screens and see if there is one active of the
# desired name Notice...this actually doesn't confirm that the
# server process is successfully running, it just checks that
# the screen is active.
if [[ `screen -ls |grep "$NAME"` ]]; then
echo "$DESC is online"
# If there is no screen of that name active, assume the server is
# offline.
else
echo "$DESC seems to be offline"
fi
}
case "$1" in
# Automatically reboot the server if script is passed "serverrestart" flag
serverrestart)
STOP && STARTS || exit 1
;;
# Automatically stop the server if script is passed "serverstop" flag
serverstop)
STOP || exit 1
;;
# Automatically start the server if script is passed "serverstart" flag
serverstart)
STARTS || exit 1
;;
# Print server status if script is passed "serverstatus" flag
serverstatus)
STATUS || exit 1
;;
# This is the menu configuration you see when you run the script if it is
# called with no flags
*)
while :
do
clear
echo " M A I N - M E N U"
echo "$DESC"
echo "1. Start $DESC"
echo "2. Stop $DESC"
echo "3. Restart $DESC"
echo "4. Say on $DESC"
echo "5. Login into $DESC"
echo "6. Serverstatus"
echo "7. Exit"
echo -n "Please enter option [1 - 8]"
read opt
case $opt in
1) STARTS
exit 1;;
2) STOP
exit 1;;
3) STOP && STARTS
exit 1;;
4) echo "You are going to write on the Server:";
read say1
SAYS
exit 1;;
5) SCREENS
exit 1;;
6) STATUS
exit 1;;
7) echo "Bye $USER";
exit 1;;
*) echo "$opt is an invaild option.";
echo -n "Please select option between [1 - 8] only";
echo "Press [enter] key to continue. . .";
read enterKey;;
esac
done
esac
For the record, we're not putting this into production yet; I'll be testing this on the test server before deploying it live. We'll still have the old one which is working if we need it.
This entry was posted on Thursday, October 18th, 2012 at 3:03 pm by surfrock66 and is filed under Owner's Corner.
You can leave a response, or trackback from your own site.
One Response to “Owner’s Corner: Start Script Part 2”
Slick. Nice work, Joe.