Last active
January 25, 2024 14:16
-
-
Save Jofkos/86a4936c91999082d6a7 to your computer and use it in GitHub Desktop.
Minecraft server tmux startscript
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/sh | |
### BEGIN INIT INFO | |
# Provides: Minecraft-Server | |
# Required-Start: $all | |
# Required-Stop: $remote_fs $syslog $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Starts minecraft server at boot time | |
# Description: Starts minecraft server at boot time | |
### END INIT INFO | |
#dir which contains the server directory | |
basedir=/home/server/minecraft/ | |
#tmux session name (`basename \"$basedir\"` -> basedir's name) | |
session="`basename \"$basedir\"`" | |
if [[ basedir != */ ]] | |
then | |
basedir+="/" | |
fi | |
start() { | |
tmux new-session -d -s $session | |
echo "Starting server" | |
tmux send-keys -t $session:0 "cd $basedir" C-m | |
tmux send-keys -t $session:0 "bash start.sh" C-m | |
echo "Server started. Attaching session..." | |
sleep 0.5 | |
tmux attach-session -t $session:0 | |
} | |
stop() { | |
tmux send-keys -t $session:0 "stop" C-m | |
echo "Stopping server..." | |
for i in {5..1..-1} | |
do | |
echo -ne "\rWaiting for shutdown... $i" | |
sleep 1 | |
done | |
echo "" | |
echo "Killing tmux session" | |
tmux kill-session -t $session | |
echo "Server stopped" | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
attach) | |
tmux attach -t $session | |
;; | |
restart) | |
stop | |
sleep 0.8 | |
echo "Restarting server..." | |
sleep 0.8 | |
start | |
;; | |
*) | |
echo "Usage: start.sh (start|stop|restart|attach)" | |
;; | |
esac |
I'm now extended it to also run a back up script which I can post here. Thanks again
#!/bin/sh
### BEGIN INIT INFO
# Provides: Minecraft-Server
# Required-Start: $all
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts minecraft server at boot time
# Description: Starts minecraft server at boot time
### END INIT INFO
#dir which contains the server directory
user=your_user
basedir=/home/$user/
backup_path="/home/$user/backups/"
folder_name=$(date +%Y-%m-%d_%H-%M-%S)
world=world
#tmux session name (`basename \"$basedir\"` -> basedir's name)
session="`basename \"$basedir\"`"
if [[ basedir != */ ]]
then
basedir+="/"
fi
start() {
tmux new-session -d -s $session
echo "Starting server"
tmux send-keys -t $session:0 "cd $basedir" C-m
tmux send-keys -t $session:0 "bash startup.sh" C-m
echo "Server started. Attaching session..."
sleep 0.5
tmux attach-session -t $session:0
}
stop() {
tmux send-keys -t $session:0 "/say Server restarting in 5 minutes. Please get ready to log off." Enter
# Wait for 1770 seconds (29 minutes and 30 seconds)
# sleep 300s
sleep 10s
tmux send-keys -t $session:0 "stop" C-m
echo "Stopping server..."
for i in {5..1..-1}
do
echo -ne "\rWaiting for shutdown... $i"
sleep 1
done
echo ""
echo "Killing tmux session"
tmux kill-session -t $session
echo "Server stopped"
}
backup() {
echo "Will start backup from $basedir to $backup_path/$folder_name..."
echo "Saving the game..."
tmux send-keys -t $session "/say Beep boop, backup starting..." Enter
tmux send-keys -t $session "save-off" Enter
tmux send-keys -t $session "save-all" Enter
sleep 30
echo "Copying the files..."
tmux send-keys -t $session "/say Beep boop, save the world..." Enter
cp -r $basedir/$world/ $backup_path/$folder_name/
echo "Removing the old backups..."
if [ $(ls $backup_path | wc -l) -gt 10 ]
then
echo "Remove 1 oldest backup..."
rm "$backup_path/$(ls $backup_path -t | tail -1)" -rf
fi
tmux send-keys -t $session "save-on" Enter
tmux send-keys -t $session "/say Beep boop, backup finished!" Enter
echo "Backup success!"
}
reload() {
tmux send-keys -t $session:0 "stop" C-m
echo "Stopping server..."
for i in {5..1..-1}
do
echo -ne "\rWaiting for shutdown... $i"
sleep 1
done
echo ""
echo "Killing tmux session"
tmux kill-session -t $session
echo "Server stopped"
}
case "$1" in
start)
start
;;
stop)
stop
;;
attach)
tmux attach -t $session
;;
restart)
backup
sleep 10
stop
sleep 0.8
echo "Restarting server..."
sleep 0.8
start
;;
reload)
stop
sleep 0.8
echo "Reload server..."
sleep 0.8
start
;;
*)
echo "Usage: start.sh (start|stop|backup|restart|reload|attach)"
;;
esac
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Completely forgot this even existed, but glad it is still useful to someone!