Ok, here is a shutdown script which I tested on Ubuntu Server 14.04 LTS but should work for most other flavors of Linux since it uses Bash.
EDIT: Yes, the same thing can be done with just a few lines of code but I like to standardize my scripts and make them re-usable as well as make it where as little needs to be changed in the script as practical.
Create a file such as 7dtd-shutdown.sh and set permissions on it:
Code:
mkdir -p /var/scripts
touch /var/scripts/7dtd-shutdown.sh
chown root:root /var/scripts/7dtd-shutdown.sh
chmod 755 /var/scripts/7dtd-shutdown.sh
Add the following text to the script:
Code:
#!/bin/bash
#############################################
## Name : 7dtd-shutdown.sh
## Version : 1.0
## Date : 2016-01-18
## Author : LHammonds
## Purpose : Warn players and shutdown game.
## Compatibility : Verified on Ubuntu Server 14.04 LTS
## Requirements : Must be run as root.
## Parameters : #1 = 7dtd server instance name (required)
## Run Frequency : Can run as often as needed.
## Exit Codes : None
################ CHANGE LOG #################
## DATE WHO WHAT WAS CHANGED
## ---------- --- ----------------------------
## 2016-01-18 LTH Created script.
#############################################
## Import standard variables and functions. ##
. /usr/local/lib/7dtd/common.sh
LogFile="/var/log/7dtd-shutdown.log"
## Requirement Check: Script must run as root user.
if [ "$(id -u)" != "0" ]; then
## FATAL ERROR DETECTED: Document problem and terminate script.
echo -e "\nERROR: Root user required to run this script.\n"
echo -e "Type 'sudo su' to temporarily become root user.\n"
exit
fi
## Check parameter.
if [ "${1}" == "" ]; then
echo -e "[ERROR] Missing required parameter. Enter the server instance."
echo -e "Syntax: ${ScriptName} [serverInstance]"
echo -e "Example 1: ${ScriptName} alpha13"
echo -e "Example 2: ${ScriptName} hamcraft"
echo -e "Here is a list of valid instances:"
7dtd.sh instances list
exit 1
else
InstanceName="${1}"
fi
telnetCommand ${InstanceName} "say \"Server shutdown in 1 minute\""
sleep 30
telnetCommand ${InstanceName} "say \"Server shutdown in 30 seconds\""
sleep 20
telnetCommand ${InstanceName} "say \"Server shutdown in 10 seconds\""
sleep 7
telnetCommand ${InstanceName} "say \"Server shutdown in 3 seconds\""
sleep 1
telnetCommand ${InstanceName} "say \"Server shutdown in 2 seconds\""
sleep 1
telnetCommand ${InstanceName} "say \"Server shutdown in 1 second\""
sleep 1
telnetCommand ${InstanceName} "say \"Be right back!\""
sleep 1
telnetCommand ${InstanceName} "shutdown"
echo "`date +%Y-%m-%d_%H:%M:%S` - [iNFO] Shutting down ${InstanceName} instance." | tee -a ${LogFile}
You can then schedule it in the root account (accessible by doing "sudo su" from your admin account) and then type "crontab -e"
Example crontab schedule:
Code:
########################################
# Name: Crontab Schedule for root user
# Author: LHammonds
############# Update Log ###############
# 2016-01-18 - LTH - Created schedule
########################################
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Crontab SYNTAX:
# minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0-6) command-to-execute
#
# Adjust the time clock
#
0 1-23 * * * /usr/sbin/ntpdate ntp.ubuntu.com > /dev/null 2>&1
#
# Shutdown 7dtd instances for daily reboot:
#
0 3 * * * /var/scripts/7dtd-shutdown.sh HamCraft > /dev/null 2>&1
0 3 * * * /var/scripts/7dtd-shutdown.sh WalkingDead > /dev/null 2>&1
0 3 * * * /var/scripts/7dtd-shutdown.sh Vanilla > /dev/null 2>&1
2 3 * * * /var/scripts/reboot.sh
Summary of what it is doing:
The schedule is configured to run the script 3 times simultaneously at 3:00am to shutdown 3 instances of servers called "HamCraft", "WalkingDead" and "Vanilla"
The shutdown of the 7dtd instances should take just a little over 1 minute so a server-wide reboot script is scheduled to go off at 3:02am
While connected to one of the servers during this period, players will get a notice that the server will be shutdown in 1 minute. Then get another notice 30 seconds later, etc. and eventually get disconnected automatically when the instance is shutdown.
LHammonds