Skip to content

Instantly share code, notes, and snippets.

@aschuhardt
Created September 23, 2020 20:04
Show Gist options
  • Save aschuhardt/2694861f662e9b08f58641b1e1c5734f to your computer and use it in GitHub Desktop.
Save aschuhardt/2694861f662e9b08f58641b1e1c5734f to your computer and use it in GitHub Desktop.
script to set up automatic reboots for minecraft server in a screen session
launch_dir="$HOME/rev3"
launch_script="$launch_dir/LaunchServer.sh"
session_name="ftb-server-$(date '+%s')"
command_prefix="screen -S $session_name -p 0 -X stuff"
crontab_file="/tmp/.cronjobs-$(date '+%s')"
echo "Launch directory: $launch_dir"
echo "Launch script: $launch_script"
echo "Session name: $session_name"
# dump existing cron jobs to file
crontab -l > $crontab_file
# clean up any cron jobs from prior runs
sed --in-place '/ftb-server-/d' $crontab_file
# create cron jobs for notifying players and stopping/restarting the server
echo "0 */6 * * * $command_prefix 'say Server is rebooting in 5 minutes^M' >/dev/null 2>&1" >> $crontab_file
echo "2 */6 * * * $command_prefix 'say Server is rebooting in 3 minutes^M' >/dev/null 2>&1" >> $crontab_file
echo "4 */6 * * * $command_prefix 'say Server is rebooting in 1 minute^M' >/dev/null 2>&1" >> $crontab_file
echo "5 */6 * * * $command_prefix 'stop^M' && sleep 10 && killall -s SIGKILL java >/dev/null 2>&1" >> $crontab_file
echo "7 */6 * * * cd $launch_dir && screen -dmS $session_name $launch_script" >> $crontab_file
# load cron jobs and remove temp file
crontab $crontab_file
rm $crontab_file
# start server in a new screen instance
pushd $launch_dir
screen -dmS $session_name $launch_script
popd
# output session name to temp file so that we can kill it later
echo "$session_name" > /tmp/ftb-session-name
@aschuhardt
Copy link
Author

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