Advertising
- Minecraft Server
- Tuesday, September 20th, 2011 at 8:23:42am MDT
- #!/bin/bash
- # This script is public domain
- COMM_FILE="/tmp/minecraft.not-a-socket"
- CONFIG_PATH="/home/minecraft/server"
- SERVER_JAR="/home/minecraft/server/minecraft_server.jar"
- MEMORY_MAX="1024M"
- MEMORY_MIN="128M"
- PID_FILE="/tmp/minecraft.pid"
- JAVA="$(which java)"
- function start_server
- {
- server_status > /dev/null && echo "Server already running." && exit 1
- # Prepare the path for server launch
- if mkdir -p "$CONFIG_PATH" && cd "$CONFIG_PATH" && > "$COMM_FILE"
- then
- # Pipe the contents of $COMM_FILE into the server every 250ms
- tail -fs 0.25 "$COMM_FILE" | \
- $JAVA -Xms$MEMORY_MIN -Xmx$MEMORY_MAX -jar "$SERVER_JAR" nogui \
- > /dev/null 2>&1 &
- # Save the PID the disown the tail and java thread
- PID="$!"
- echo "$PID" > "$PID_FILE"
- disown "$PID"
- fi
- server_status
- return "$?"
- }
- function stop_server
- {
- server_status > /dev/null || { echo "Server not running" && exit 1; }
- # Send "stop" to the server console
- communicate stop
- # Kill off any tail commands reading from the communication file
- TAILS="$(ps ao comm,pid,args | \
- awk "/^tail.*$(basename $COMM_FILE)\$/ {print \$2}")"
- kill $TAILS > /dev/null 2>&1
- echo -n "Waiting on server to stop..."
- while server_status > /dev/null
- do
- echo -n "."
- sleep 1
- done
- echo " Done."
- }
- function communicate
- {
- if server_status > /dev/null
- then
- # Save the last line from the server log
- LAST_LINE="$(tail -n1 "$CONFIG_PATH/server.log")"
- # Echo the message to the $COMM_FILE
- echo $* >> "$COMM_FILE"
- # Sleep for a moment, then print all new dat written to the server log
- usleep 250000 > /dev/null 2>&1 || sleep 1
- fgrep -A100000 "$LAST_LINE" "$CONFIG_PATH/server.log" | tail -n +2
- return 0
- fi
- echo "Unable to communicate with server."
- return 1
- }
- function server_status
- {
- PID="$(cat "$PID_FILE" 2> /dev/null)"
- if kill -0 "$PID" > /dev/null 2>&1
- then
- echo "Server PID is $PID."
- return 0
- fi
- echo "Server is not running."
- return 1
- }
- case "$1" in
- start)
- start_server
- ;;
- stop)
- stop_server
- ;;
- restart)
- stop_server
- start_server
- ;;
- status)
- server_status
- ;;
- send)
- shift
- communicate $*
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status|send MESSAGE}"
- exit 1
- ;;
- esac
- exit
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.