|
#!/bin/bash |
|
# /etc/init.d/mcl |
|
|
|
### 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: LilyPad Connect Launcher |
|
# Description: Init script for LilyPad Connect, with rolling logs. |
|
### END INIT INFO |
|
|
|
# Go Lily Pad Launcher |
|
# Created by Roblabla |
|
|
|
############################# |
|
######### SETTINGS ########## |
|
############################# |
|
# Pretty Name to display in messages |
|
SERVICE="LilyPad Connect" |
|
|
|
# Name of Go LilyPad file |
|
FILE="connect-linux-amd64" |
|
ARGS="" |
|
|
|
# Name to use for the screen instance |
|
SCREEN="glpc" |
|
|
|
# User that should run the server |
|
USERNAME="minecraft-test" |
|
|
|
# Path to Go LilyPad directory |
|
FILEPATH="/home/minecraft-test/glpc" |
|
|
|
# Log Paths |
|
LOG="$FILEPATH/glpc.log" |
|
LOGROLL="$FILEPATH/logs/" |
|
|
|
# Add start parameters |
|
INVOCATION="$FILE $ARGS | tee $LOG" |
|
|
|
# Where the world backups should go |
|
#BACKUPFILEPATH="/opt/servers/backups/worlds" |
|
|
|
# Where the whole minecraft directory is copied when whole-backup is executed |
|
#WHOLEBACKUP="/opt/servers/backups" |
|
|
|
# Where the worlds are located on the disk. Can not be the same as MCFILEPATH. |
|
#WORLDSTORAGE="${MCFILEPATH}/worldstorage" |
|
|
|
# Path to the the mounted ramdisk default in Ubuntu: /dev/shm |
|
#RAMDISK="/dev/shm" |
|
|
|
# use tar or zip files for world backup |
|
#BACKUPFORMAT="tar" |
|
|
|
############################# |
|
###### END OF SETTINGS ###### |
|
############################# |
|
|
|
APPVERSION="v1.0" |
|
|
|
echo -e "\e[00;32m$SERVICE Launcher $APPVERSION" |
|
echo "-------------------------" |
|
echo -ne "\e[00m" |
|
|
|
ME=$(whoami) |
|
as_user() { |
|
if [ "$ME" == "$USERNAME" ] ; then |
|
bash -c "$1" |
|
else |
|
su - $USERNAME -c "$1" |
|
fi |
|
} |
|
datepath() { |
|
# datepath path filending-to-check returned-filending |
|
if [ -e $1`date +%F`$2 ] |
|
then |
|
echo $1`date +%FT%T`$3 |
|
else |
|
echo $1`date +%F`$3 |
|
fi |
|
} |
|
is_running() { |
|
# Checks for the minecraft servers screen session |
|
# returns true if it exists. |
|
pidfile=${FILEPATH}/${SCREEN}.pid |
|
|
|
if [ -f "$pidfile" ] |
|
then |
|
pid=$(head -1 $pidfile) |
|
if ps ax | grep -v grep | grep ${pid} | grep "${SCREEN}" > /dev/null |
|
then |
|
return 0 |
|
else |
|
if [ -z "$isInStop" ] |
|
then |
|
if [ -z "$roguePrinted" ] |
|
then |
|
roguePrinted=1 |
|
echo "Rogue pidfile found!" |
|
fi |
|
fi |
|
return 1 |
|
fi |
|
else |
|
if ps ax | grep -v grep | grep "${SCREEN} ${FILE}" > /dev/null |
|
then |
|
echo "No pidfile found, but server's running." |
|
echo "Re-creating the pidfile." |
|
|
|
pid=$(ps ax | grep -v grep | grep "${SCREEN} ${FILE}" | cut -f1 -d' ') |
|
check_permissions |
|
as_user "echo $pid > $pidfile" |
|
|
|
return 0 |
|
else |
|
return 1 |
|
fi |
|
fi |
|
} |
|
check_permissions() { |
|
as_user "touch $1" |
|
if ! as_user "test -w '$1'" ; then |
|
echo "Check Permissions. Cannot write to $1. Correct the permissions and then excute: $0 status" |
|
fi |
|
} |
|
glpc_start() { |
|
servicefile="$FILEPATH/$FILE" |
|
if [ ! -f "$servicefile" ] |
|
then |
|
echo "Failed to start: Can't find the specified $FILE under $servicefile. Please check your config." |
|
exit 1 |
|
fi |
|
|
|
pidfile=${FILEPATH}/${SCREEN}.pid |
|
check_permissions $pidfile |
|
|
|
as_user "cd $FILEPATH && screen -dmS $SCREEN bash -c \"$INVOCATION\"" |
|
as_user "screen -list | grep "\.$SCREEN" | cut -f1 -d'.' | tr -d -c 0-9 > $pidfile" |
|
|
|
# |
|
# Waiting for the server to start |
|
# |
|
seconds=0 |
|
until is_running |
|
do |
|
sleep 1 |
|
seconds=$seconds+1 |
|
if [[ $seconds -eq 5 ]] |
|
then |
|
echo "Still not running, waiting a while longer..." |
|
fi |
|
if [[ $seconds -ge 120 ]] |
|
then |
|
echo "Failed to start, aborting." |
|
exit 1 |
|
fi |
|
done |
|
echo "$SERVICE is running." |
|
} |
|
glpc_command() { |
|
command="$@" |
|
if is_running |
|
then |
|
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"$command\"\015'" |
|
else |
|
echo "$SERVICE was not running. Not able to run command." |
|
fi |
|
} |
|
glpc_stop() { |
|
pidfile=${FILEPATH}/${SCREEN}.pid |
|
# |
|
# Stops the server |
|
# |
|
echo "Stopping server..." |
|
glpc_command stop |
|
sleep 0.5 |
|
# |
|
# Waiting for the server to shut down |
|
# |
|
seconds=0 |
|
isInStop=1 |
|
while is_running |
|
do |
|
sleep 1 |
|
seconds=$seconds+1 |
|
if [[ $seconds -eq 5 ]] |
|
then |
|
echo "Still not shut down, waiting a while longer..." |
|
fi |
|
if [[ $seconds -ge 120 ]] |
|
then |
|
logger -t minecraft-init "Failed to shut down server, aborting." |
|
echo "Failed to shut down, aborting." |
|
exit 1 |
|
fi |
|
done |
|
as_user "rm $pidfile" |
|
unset isInStop |
|
is_running |
|
echo "$SERVICE is now shut down." |
|
} |
|
|
|
log_roll() { |
|
as_user "mkdir -p $LOGROLL" |
|
path=`datepath $LOGROLL/server_ .log.gz .log` |
|
as_user "mv $LOG/server.log $path && gzip $path" |
|
as_user "touch $LOG" |
|
} |
|
|
|
function ask { |
|
while true; do |
|
|
|
if [ "${2:-}" = "Y" ]; then |
|
prompt="Y/n" |
|
default=Y |
|
elif [ "${2:-}" = "N" ]; then |
|
prompt="y/N" |
|
default=N |
|
else |
|
prompt="y/n" |
|
default= |
|
fi |
|
|
|
# Ask the question |
|
read -p "$1 [$prompt] " REPLY |
|
|
|
# Default? |
|
if [ -z "$REPLY" ]; then |
|
REPLY=$default |
|
fi |
|
|
|
# Check if the reply is valid |
|
case "$REPLY" in |
|
Y*|y*) return 0 ;; |
|
N*|n*) return 1 ;; |
|
esac |
|
|
|
done |
|
} |
|
|
|
case "$1" in |
|
start) |
|
glpc_start |
|
;; |
|
stop) |
|
# Stops the server |
|
glpc_stop |
|
;; |
|
restart) |
|
glpc_stop |
|
glpc_start |
|
;; |
|
reload) |
|
# Reload the server and plugins |
|
glpc_command reload |
|
;; |
|
console) |
|
echo "This command will load the screen that the server is running under" |
|
echo "To exit the screen press CTRL+A D" |
|
|
|
if ask "Do you wish to load the screen?" N; |
|
then |
|
as_user "script /dev/null -c 'screen -p 0 -S $SCREEN -dr'" |
|
fi |
|
;; |
|
#connected) |
|
# Lists connected users |
|
#as_user "screen -p 0 -S $SCREEN -X eval 'stuff online\015'" |
|
#echo "Working..." |
|
#sleep 1s |
|
#echo -ne \\033[1A |
|
#echo "Currently Connected:" |
|
#tac $MCFILEPATH/server.log | grep -m 1 "Connected" | sed "s|.*Connected players:||" | sed "s|, |\n|g" |
|
#;; |
|
log-roll) |
|
# Moves and Gzips the logfile |
|
log_roll |
|
;; |
|
#last) |
|
# greps for recently logged in users |
|
#echo Recently logged in users: |
|
#cat $LOG | awk '/entity|conn/ {sub(/lost/,"disconnected");print $1,$2,$4,$5}' |
|
#;; |
|
restartifdead) |
|
if is_running |
|
then |
|
echo "All is fine" |
|
else |
|
glpc_stop |
|
log_roll |
|
glpc_start |
|
fi |
|
;; |
|
status) |
|
# Shows server status |
|
if is_running |
|
echo "$SERVER is running" |
|
#getusage_cpu |
|
#getusage_ram |
|
else |
|
echo "$SERVICE is not running." |
|
exit 1 |
|
fi |
|
;; |
|
#livestatus) |
|
# Shows server status, live... |
|
#if as_user "ps x | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null" |
|
#then |
|
#echo "Live $SERVICE Stats..." |
|
#echo "Press any key to quit..." |
|
#echo "-------------------------------------------------------------" |
|
#echo "CPU" |
|
#echo "RAM" |
|
|
|
#stty -echo |
|
#while : ; do |
|
#echo -ne \\033[2A |
|
#getusage_cpu |
|
#getusage_ram |
|
#read -t 1 -n 1 keypress |
|
#if [ $? -eq 0 ] |
|
#then |
|
#break |
|
#fi |
|
#done |
|
#stty echo |
|
#else |
|
# echo "$SERVICE is not running." |
|
#fi |
|
#;; |
|
#version) |
|
#echo Craftbukkit version `awk '/Craftbukkit/ {sub(/\)/, ""); print $12}' $MCFILEPATH/server.log` |
|
#;; |
|
exec) |
|
glpc_command $2 |
|
;; |
|
help) |
|
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" |
|
echo "Usage: ./$me command" |
|
echo "----------------------------------------------------------------------" |
|
echo "start - Starts the server" |
|
echo "stop - stops the server" |
|
echo "restart - restarts the server" |
|
echo "restartifdead - restarts the server if it's not running" |
|
echo "reload - reload the server" |
|
echo "console - Access the server console" |
|
echo "log-roll - Moves and gzips the logfile" |
|
echo "status - Shows server status" |
|
echo "version - returs $SERVICE version" |
|
#echo "say [\"MESSAGE\"] - say a message as server" |
|
echo "exec [\"COMMAND\"] - execute command on server" |
|
;; |
|
*) |
|
me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" |
|
echo "No such command see ./$me help" |
|
exit 1 |
|
;; |
|
esac |
|
|
|
#notes |
|
#2012-01-10 09:50:04 [INFO] Done (0.997s)! For help, type "help" or "?" |
|
|
|
|
|
exit 0 |