Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Minecraft Server
Tuesday, September 20th, 2011 at 8:23:42am MDT 

  1. #!/bin/bash
  2. # This script is public domain
  3.  
  4. COMM_FILE="/tmp/minecraft.not-a-socket"
  5. CONFIG_PATH="/home/minecraft/server"
  6. SERVER_JAR="/home/minecraft/server/minecraft_server.jar"
  7. MEMORY_MAX="1024M"
  8. MEMORY_MIN="128M"
  9. PID_FILE="/tmp/minecraft.pid"
  10. JAVA="$(which java)"
  11.  
  12. function start_server
  13. {
  14.     server_status > /dev/null && echo "Server already running." && exit 1
  15.  
  16.     # Prepare the path for server launch
  17.     if mkdir -p "$CONFIG_PATH" && cd "$CONFIG_PATH" && > "$COMM_FILE"
  18.     then
  19.         # Pipe the contents of $COMM_FILE into the server every 250ms
  20.         tail -fs 0.25 "$COMM_FILE" | \
  21.             $JAVA -Xms$MEMORY_MIN -Xmx$MEMORY_MAX -jar "$SERVER_JAR" nogui \
  22.             > /dev/null 2>&1 &
  23.  
  24.         # Save the PID the disown the tail and java thread
  25.         PID="$!"
  26.         echo "$PID" > "$PID_FILE"
  27.         disown "$PID"
  28.     fi
  29.  
  30.     server_status
  31.     return "$?"
  32. }
  33.  
  34. function stop_server
  35. {
  36.     server_status > /dev/null || { echo "Server not running" && exit 1; }
  37.  
  38.     # Send "stop" to the server console
  39.     communicate stop
  40.  
  41.     # Kill off any tail commands reading from the communication file
  42.     TAILS="$(ps ao comm,pid,args | \
  43.         awk "/^tail.*$(basename $COMM_FILE)\$/ {print \$2}")"
  44.     kill $TAILS > /dev/null 2>&1
  45.  
  46.     echo -n "Waiting on server to stop..."
  47.     while server_status > /dev/null
  48.     do
  49.         echo -n "."
  50.         sleep 1
  51.     done
  52.     echo " Done."
  53. }
  54.  
  55. function communicate
  56. {
  57.     if server_status > /dev/null
  58.     then
  59.         # Save the last line from the server log
  60.         LAST_LINE="$(tail -n1 "$CONFIG_PATH/server.log")"
  61.  
  62.         # Echo the message to the $COMM_FILE
  63.         echo $* >> "$COMM_FILE"
  64.  
  65.         # Sleep for a moment, then print all new dat written to the server log
  66.         usleep 250000 > /dev/null 2>&1 || sleep 1
  67.         fgrep -A100000 "$LAST_LINE" "$CONFIG_PATH/server.log" | tail -n +2
  68.         return 0
  69.     fi
  70.  
  71.     echo "Unable to communicate with server."
  72.     return 1
  73. }
  74.  
  75. function server_status
  76. {
  77.     PID="$(cat "$PID_FILE" 2> /dev/null)"
  78.     if kill -0 "$PID" > /dev/null 2>&1
  79.     then
  80.         echo "Server PID is $PID."
  81.         return 0
  82.     fi
  83.  
  84.     echo "Server is not running."
  85.     return 1
  86. }
  87.  
  88. case "$1" in
  89.     start)
  90.         start_server
  91.         ;;
  92.     stop)
  93.         stop_server
  94.         ;;
  95.     restart)
  96.         stop_server
  97.         start_server
  98.         ;;
  99.     status)
  100.         server_status
  101.         ;;
  102.     send)
  103.         shift
  104.         communicate $*
  105.         ;;
  106.     *)
  107.         echo "Usage: $0 {start|stop|restart|status|send MESSAGE}"
  108.         exit 1
  109.         ;;
  110. esac
  111. exit

Paste Details

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right fantasy-obligation
fantasy-obligation