Last active
August 3, 2017 15:04
-
-
Save maxaudron/ed6184eb1588c2999ceb43639df5d443 to your computer and use it in GitHub Desktop.
Script for managing game servers using tmux to run in the background
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 | |
# --------------------------------------------- | |
# Script for starting and stoping game servers | |
# start | stop | restart | status | console | |
# --------------------------------------------- | |
# Exit Codes: | |
# 0 = running | |
# 1 = stopped | |
# 2 = other | |
################################################ | |
################### Settings ################### | |
################################################ | |
# Game Name | |
# Can be anything. Used for feedback in the console | |
GAMENAME="Just Cause 3" | |
# Game Shorthand | |
# Used to identify server process. Recommended to set to same as name of this script | |
GAMEID="jc3server" | |
# Server Executable | |
# Name of the executable server file | |
# Can be used with flags: SERVEREXEC="Server -flag1" | |
SERVEREXEC="Server" | |
# Exit Command | |
# Command to stop the server | |
GAMEEXIT="exit" | |
# Config Commands | |
# Gets passed to exec | |
CONFIG="" | |
################################################ | |
#################### Script #################### | |
################ Edit with care ################ | |
################################################ | |
GAMEPID=$(pidof $GAMEID) | |
startme() { | |
echo -e "[\033[0;33m ... \033[0m] Starting $GAMENAME Server" | |
case "$(pidof $GAMEID | wc -w)" in | |
0) tmux new-session -d -s $GAMEID 'bash -c "exec -a $GAMEID ./$SERVEREXEC $CONFIG"' | |
echo -e "[\033[0;32m Done \033[0m] $GAMENAME Server started" | |
exit 0 | |
;; | |
1) echo -e "[\033[0;31m Failed \033[0m] $GAMENAME Server already running" | |
exit 2 | |
;; | |
esac | |
} | |
stopme() { | |
echo -e "[\033[0;33m ... \033[0m] Stopping $GAMENAME Server" | |
case "$(pidof $GAMEID | wc -w)" in | |
0) echo -e "[\033[0;31m Failed \033[0m] $GAMENAME Server is already stopped" | |
exit 2 | |
;; | |
1) tmux send-keys -t $GAMEID C-z $GAMEEXIT Enter | |
end=$((SECONDS+30)) | |
secs=$((30)) | |
while [ $SECONDS -lt $end ]; do | |
case "$(pidof $GAMEID | wc -w)" in | |
0) echo -e "[\033[0;32m Done \033[0m] $GAMENAME Server stopped" | |
exit 1 | |
;; | |
1) echo -ne "[\033[0;33m ... \033[0m] Waiting for graceful exit: $secs\033[0K\r" | |
sleep 1 | |
secs=$((secs - 1)) | |
continue | |
;; | |
esac | |
: | |
done | |
echo -e "[\033[0;31m Failed \033[0m] Could not exit gracefully. Killing process" | |
kill $GAMEPID | |
echo -e "[\033[0;32m Done \033[0m] $GAMENAME Server stopped" | |
exit 1 | |
esac | |
} | |
viewme() { | |
echo -e "[\033[0;33m ... \033[0m] Accessing Console of $GAMENAME Server" | |
case "$(pidof $GAMEID | wc -w)" in | |
0) echo -e "[\033[0;31m Failed \033[0m] $GAMENAME Server is stopped" ;; | |
1) echo -e "Don't exit with\033[0;33m Ctrl-C\033[0m. This will kill the Server. Use\033[0;33m Ctrl-B D \033[0m" | |
read -p "Understood? (y/n)? " answer | |
case ${answer:0:1} in | |
y|Y ) | |
tmux attach -t $GAMEID | |
;; | |
* ) | |
exit 2 | |
;; | |
esac | |
echo -e "[\033[0;32m Done \033[0m] Exited Console" | |
;; | |
esac | |
} | |
statusme() { | |
echo -e "[\033[0;33m ... \033[0m] Checking Status of $GAMENAME Server" | |
case "$(pidof $GAMEID | wc -w)" in | |
0) echo -e "[\033[0;31m Stopped \033[0m] $GAMENAME Server is stopped" | |
exit 1 | |
;; | |
1) echo -e "[\033[0;32m Running \033[0m] $GAMENAME Server is running" | |
exit 0 | |
;; | |
esac | |
} | |
case "$1" in | |
start) startme ;; | |
stop) stopme ;; | |
restart) stopme; startme ;; | |
console) viewme ;; | |
status) statusme ;; | |
*) | |
echo -e "Usage: $GAMEID start|stop|restart|status|console" | |
exit 2 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment