All pastes #2081576 Raw Edit

Minecraft Server

public shellscript v1 · immutable
#2081576 ·published 2011-09-20 14:23 UTC
rendered paste body
#!/bin/bash# This script is public domainCOMM_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        ;;esacexit