Skip to content

Instantly share code, notes, and snippets.

@joelouisworthington
Last active February 6, 2024 03:22
Show Gist options
  • Save joelouisworthington/c7d8e1d3411bb4601a1449ce9b69c685 to your computer and use it in GitHub Desktop.
Save joelouisworthington/c7d8e1d3411bb4601a1449ce9b69c685 to your computer and use it in GitHub Desktop.
tmux Minecraft start, stop, restart, reload and backup script
#!/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
@joelouisworthington
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment