#!/bin/bash say() { echo "$1" mcbe-exec say "$1" } # first argument gives if server should halt or restart if [[ -z $1 || $1 == "-h" ]]; then restart=1 elif [[ $1 == "-r" ]]; then restart=0 else echo "Invalid value for shutdown type: acceptable values are \"-h\" for halt, or \"-r\" for restart." exit 2 fi # 2nd argument gives custom warning time if [[ -z $2 ]]; then # default warning time is 10 seconds time=10 elif [[ $1 -ge 0 && $1 -le 60 ]]; then time=$1 else echo "Invalid value for time-to-shutdown: only values between 0 and 60 seconds are accepted." exit 2 fi say "Server shutting down in $time seconds!" count=$(($time-1)) sleep 1 while [[ $count -gt 0 ]]; do # print warning every 10 seconds if [[ $(( $count % 10 )) -eq 0 ]]; then say "Server shutting down in ${count} seconds!" fi # print warning every second for last 5 seconds if [[ $count -le 5 ]]; then say "Shutting down in ${count}..." fi count=$((count-1)) sleep 1 done systemctl stop mcbe if [[ $restart ]]; then echo "Starting server..." sleep 1 systemctl start mcbe sleep 3 fi echo "Done!"