-
-
Save cking/9809423 to your computer and use it in GitHub Desktop.
minecraft runscript
This file contains 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 | |
USERNAME="minecraft" | |
SERVICE='forge.jar' | |
JAVA_OPTS="-Xmx8G -Xms8G" | |
MCPATH="/srv/$USERNAME" | |
BACKUPPATH="$MCPATH/backup" | |
BACKUPARCHIVEPATH=$BACKUPPATH/archive | |
INVOCATION="/usr/bin/java ${JAVA_OPTIONS} -jar $SERVICE nogui" | |
PORT=$(grep server-port $MCPATH/server.properties | cut -d '=' -f 2) | |
if [ -z "$PORT" ]; then | |
PORT=25565 | |
fi | |
if [ $(whoami) != $USERNAME ]; then | |
su $USERNAME -l -c "$(readlink -f $0) $*" | |
exit $? | |
fi | |
is_running() { | |
if `tmux has-session -t mc$PORT 2> /dev/null`; then | |
list=$(tmux lsp -F '#{session_name}' | grep "mc$PORT" | sort -r) | |
for line in $list; do | |
pid=`echo $line | cut -d: -f2` | |
app=`ps p $pid | tail -1 | awk '{print $5}'` | |
if `echo $app | grep $INVOCATION > /dev/null ; echo $?`; then | |
return 0 | |
fi | |
done | |
return 1 | |
else | |
return 1 | |
fi | |
} | |
mc_start() { | |
if is_running; then | |
echo "Tried to start but $SERVICE was already running!" | |
else | |
echo "$SERVICE was not running... starting." | |
cd $MCPATH | |
tmux new-session -d -n mc$PORT -s mc$PORT $INVOCATION | |
fi | |
} | |
mc_stop() { | |
if is_running; then | |
echo "$SERVICE is running... stopping." | |
mc_exec "say SERVER SHUTTING DOWN IN 10 SECONDS! Saving map..." | |
mc_exec "save-all" | |
sleep 10 | |
mc_exec "stop" | |
for (( i=0; i < 20; i++ )); do | |
is_running || break | |
echo -n . | |
sleep 1 | |
done | |
echo | |
else | |
echo "$SERVICE was not running." | |
fi | |
if is_running; then | |
echo "$SERVICE could not be shut down cleanly... still running." | |
mc_kill | |
else | |
echo "$SERVICE is shut down." | |
fi | |
} | |
mc_kill() { | |
pid=$(cat $MCPATH/java.pid) | |
echo "terminating process with pid $pid" | |
kill $pid | |
for (( i=0; i < 10; i++ )); do | |
is_running || break | |
sleep 1 | |
done | |
if is_running; then | |
echo "$SERVICE could not be terminated, killing..." | |
kill -SIGKILL $pid | |
echo "$SERVICE killed" | |
else | |
echo "$SERVICE terminated" | |
fi | |
} | |
mc_saveoff() { | |
if is_running; then | |
echo "$SERVICE is running... suspending saves" | |
mc_exec "say SERVER BACKUP STARTING! Server going readonly..." | |
mc_exec "save-off" | |
mc_exec "save-all" | |
sleep 10 | |
else | |
echo "$SERVICE was not running. Not suspending saves." | |
fi | |
} | |
mc_saveon() { | |
if is_running; then | |
echo "$SERVICE is running... re-enabling saves" | |
mc_exec "save-on" | |
mc_exec "say SERVER BACKUP ENDED! Server going read-write..." | |
else | |
echo "$SERVICE was not running. Not resuming saves." | |
fi | |
} | |
mc_backup() { | |
echo "Backing up minecraft world" | |
backupdir=`date "+%F %T"` | |
7z a -r -y "$BACKUPPATH/$backup.7z" $MCPATH/world >/dev/null | |
echo "Backup complete" | |
} | |
mc_exec() { | |
if is_running; then | |
tmux send-keys -t mc$PORT "$@^M" | |
else | |
echo "$SERVICE was not running. Not executing command." | |
fi | |
} | |
case "$1" in | |
start) | |
mc_start | |
;; | |
stop) | |
mc_stop | |
;; | |
restart) | |
mc_stop | |
mc_start | |
;; | |
backup) | |
mc_saveoff | |
mc_backup | |
mc_saveon | |
;; | |
exec) | |
shift | |
mc_exec "$@" | |
;; | |
exec) | |
shift | |
set -- say "$@" | |
mc_exec "$@" | |
;; | |
status) | |
if is_running; then | |
echo "$SERVICE is running." | |
else | |
echo "$SERVICE is not running." | |
fi | |
;; | |
*) | |
echo "Usage: $(readlink -f $0) {start|stop|restart|backup|exec|say|status}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment