-
-
Save KevinKurtz/003aa194c5dd03fad440b587b2e21c16 to your computer and use it in GitHub Desktop.
stop (pause) inactive minecraft server process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
##I actually use this as part of my run.sh script, so I call minecraft first, but I've commented that out | |
##I run this in crontab -e as @reboot /home/minecraft/minecraft/run.sh > /dev/null 2>&1 | |
#BINDIR=$(dirname "$(readlink -fn "$0")") | |
#cd "$BINDIR" | |
#/usr/bin/screen -d -m -S mcs /usr/bin/java -Xms512M -Xmx1536M -jar server.jar n$ | |
#wait a minute before we start watchdogging the server | |
#sleep 60 | |
# set this to the port your minecraft server is on | |
PORT=25565 | |
# how frequently do we check for connections (seconds) | |
CHECK_DELAY=60 | |
# script will start the server briefly duing each cycle | |
POLL_CYCLE=600 | |
# how long to resume server during poll cycle | |
POLL_RUN=1 | |
# delay in checking for connections during poll | |
POLL_DELAY=5 | |
# If you have multiple java environments this will need to be changed | |
function find_pid(){ | |
echo $(pidof java) | |
} | |
function pause(){ | |
# this stops the process (like ctrl-z) | |
PID=$(find_pid) | |
kill -STOP $PID | |
} | |
function resume(){ | |
PID=$(find_pid) | |
#if the PID doesn't exist, we will exit so that we don't wait for a connected user forever | |
kill -CONT $PID || exit 1 | |
} | |
while true; do | |
# wait a bit before looping again | |
sleep 60 | |
echo "checking for activity on $PORT" | |
# while ( netstat -tn | grep $PORT | grep ESTABLISHED ); do | |
while ( netstat -tn | grep $PORT ); do | |
#echo "still active..." | |
sleep $CHECK_DELAY | |
done | |
echo "no active connections on $PORT, so pausing process $(find_pid)" | |
pause | |
echo "waiting for activity on $PORT, will run server for $POLL_RUN seconds every $POLL_CYCLE seconds." | |
ACTIVE="" | |
while [ -z "$ACTIVE" ]; do | |
echo "Resume process $(find_pid) for $POLL_RUN seconds to prevent crashes/timeouts" | |
resume | |
sleep $POLL_RUN | |
pause | |
echo "waiting for activity on $PORT" | |
POLL_UNTIL=$((SECONDS+POLL_CYCLE)) | |
while [ $SECONDS -lt $POLL_UNTIL ]; do | |
ACTIVE=`netstat -tn | grep $PORT` | |
#echo "ACTIVE=$ACTIVE" | |
if [ -n "$ACTIVE" ]; then | |
break | |
else | |
sleep $POLL_DELAY | |
fi | |
done | |
done; | |
echo "Resuming process $(find_pid) due to activty $ACTIVE" | |
resume | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment