Skip to content

Instantly share code, notes, and snippets.

@San3ko
Created January 29, 2012 18:06
Show Gist options
  • Save San3ko/e64e09a3f1ae6b0fd17b to your computer and use it in GitHub Desktop.
Save San3ko/e64e09a3f1ae6b0fd17b to your computer and use it in GitHub Desktop.
MControl linux
#!/bin/bash
#=================================#
# minecraft linux control script #
# v 0.4.2b by OrangeFox #
# utf8 centOS 5.6 #
# http://404team.ru #
#=================================#
# Based on http://www.minecraftwiki.net/wiki/Server_startup_script
#=========SETTINGS============================
#name of file
SERVICE="mc.jar"
#username
USERNAME="gamine"
#directory
MCPATH="/home/${USERNAME}"
#directory of backups
BACKUPPATH="/home/${USERNAME}/backups"
#directory of logs
LOGPATH="${MCPATH}/log"
#name of this file
FILENAME="mine"
#name of screen
SCREEN="mine"
#name of world
WORLDNAME="world"
#cores
CPU_COUNT=2
# Initial memory usage
INITMEM="2048M"
# Maximum amount of memory to use
MAXMEM="2048M"
#launch
LAU="java -Xmx$MAXMEM -Xms$INITMEM -XX:ParallelGCThreads=$CPU_COUNT -jar $SERVICE nogui"
#===============================================
#===============================================
case "$1" in
start)
#starting the server
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "Tried to start but $SERVICE was already running!"
else
echo "$SERVICE was not running... starting."
cd $MCPATH && screen -dmS $SCREEN $LAU
sleep 7
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Could not start $SERVICE."
fi
fi
;;
stop)
# stopping
# stopping
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... stopping."
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER WILL STOPPED IN 5 MIN!!"\015'
sleep 59s
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER WILL STOPPED IN 4 MIN!!"\015'
sleep 59s
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER WILL STOPPED IN 3 MIN!!"\015'
sleep 59s
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER WILL STOPPED IN 2 MIN!!"\015'
sleep 59s
screen -p 0 -S $SCREEN -X eval 'stuff "say 60 SECONDS LEFT!!"\015'
sleep 39s
screen -p 0 -S $SCREEN -X eval 'stuff "say SYSTEM: Svaing world and shutting down!"\015'
screen -p 0 -S $SCREEN -X eval 'stuff "save-all"\015'
sleep 10
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER OFF NOW!"\015'
screen -p 0 -S $SCREEN -X eval 'stuff "stop"\015'
sleep 7
else
echo "$SERVICE was not running."
fi
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE could not be shut down... still running."
else
echo "$SERVICE is shut down."
fi
;;
faststop)
# stop without warnings
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... stopping."
screen -p 0 -S $SCREEN -X eval 'stuff "save-all"\015'
sleep 10
screen -p 0 -S $SCREEN -X eval 'stuff "say SERVER OFF NOW!"\015'
screen -p 0 -S $SCREEN -X eval 'stuff "stop"\015'
sleep 7
else
echo "$SERVICE was not running."
fi
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE could not be shut down... still running."
else
echo "$SERVICE is shut down."
fi
;;
restart)
# restarting (too many checks, will fix in next versions)
echo "[***] Server status check! :"
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "Choose type of restart"
echo " 1 - fast restart (15sec)"
echo " 2 - slow restart (5 min, with warnings in chat"
echo "Enter number of type: "
read rtype
if [ "$rtype" == 1 ]
then
echo "=========Fast restarting, wait 20sec=========="
$0 faststop
echo "=======starting==========="
$0 start
echo "======DONE======="
else
if [ "$rtype" == 2 ]
then
echo "=====[$(date +%H:%M:%S)] 5 min to start the server...===="
$0 stop
echo "=====stop done, start======="
$0 start
echo "======DONE========"
else
echo "Wrong number, try again"
fi
fi
else
echo "$SERVICE is not running! Restart aborted"
echo -n "Are you want to start server? (y/n): "
read answ
if [ "$answ" == y ]
then
echo "Ok, starting"
echo "=========START SERVER======"
$0 start
echo "=====DONE====="
else
if [ "$answ" == Y ]
then
echo "Ok, starting"
echo "=========START SERVER======"
$0 start
echo "=====DONE====="
else
echo "ok, bye"
fi
fi
fi
;;
status)
# just check status of server and record in log
echo "checking status"
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
echo "[$(date +%d.%m.%Y)_$(date +%H:%M:%S)] Server online" >> $LOGPATH/mine.log
else
echo "$SERVICE is not running"
echo "[$(date +%d.%m.%Y)_$(date +%H:%M:%S)] Server offline" >> $LOGPATH/mine.log
fi
;;
online)
#shows connected players
screen -p 0 -S $SCREEN -X eval 'stuff list\015'
sleep 3s
tac $MCPATH/server.log | grep -m 1 "Connected"
;;
reload)
#reloading plugins
echo "send 'reload' command to server"
screen -p 0 -S $SCREEN -X eval 'stuff "reload"\015'
sleep 7s
echo "Reload complete"
;;
last)
# greps for recently logged in users
echo Recently logged in users:
cat $MCPATH/server.log | awk '/entity|conn/ {sub(/lost/,"disconnected");print $1,$2,$4,$5}'
;;
version)
#show version of server (need to fix)
echo Craftbukkit version `awk '/Craftbukkit/ {sub(/\)/, ""); print $12}' $MCPATH/server.log`
;;
backup)
# backuping current world in special folder
echo "backupping world start, wait some time"
screen -p 0 -S $SCREEN -X eval 'stuff "save-all"\015'
mkdir -p $BACKUPPATH
path="$BACKUPPATH/backup_($WORLDNAME)_[$(date +%d.%m.%Y)].tar.bz2"
tar -hcjf $path $MCPATH/$WORLDNAME
echo "Done"
;;
check)
#checking requirements folders/files (in job)
echo "Minecraft directory: $MCPATH "
echo "========Jar file"
if [ -a $MCPATH/$SERVICE ]
then
echo " $MCPATH/$SERVICE - OK "
else
echo " $MCPATH/$SERVICE - MISS "
fi
echo "========log directory"
if [ -d $LOGPATH ]
then
echo " $LOGPATH - OK "
else
echo " $LOGPATH - MISS "
fi
echo "========backup path"
if [ -d $BACKUPPATH ]
then
echo " $BACKUPPATH - OK "
else
echo " $BACKUPPATH - MISS "
fi
;;
help)
#user help
echo "Usage: $MCPATH/$FILENAME command"
echo
echo "start, stop, restart, status, online, last, version, backup"
echo "start - starting the server if it not running"
echo "stop - stopping server if it runnig"
echo "restart - restart server if it running"
echo "status - check the status of server (on/off)"
echo "online - list of currently online players"
echo "version - version of the server"
echo "backup - saving and zipping in tar current world"
echo "reload - reloading plugins and permissions"
;;
#================================================
#commands for cron
rst)
$0 stop
sleep 5s
$0 start
sleep 5s
$0 status
;;
frst)
$0 faststop
sleep 10s
$0 start
sleep 5s
$0 status
;;
#================================================
cron)
echo "easy to do commands for cron table"
echo " $MCPATH/$FILENAME rst - slow restart (6 min)"
echo " $MCPATH/$FILENAME frts - fast restart (1 min)"
*)
echo "No such command see $MCPATH/$FILENAME help"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment