How to Install 7 Days to Die on Ubuntu Server 18.04 LTS

Post Reply
User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

How to Install 7 Days to Die on Ubuntu Server 18.04 LTS

Post: # 778Post LHammonds
Sun Oct 13, 2019 7:05 pm

********** WORK IN PROGRESS **********

Overview

This article will describe how to install a dedicated 7 Days to Die server and configure it to run two instances of a map (Navezgane and a randomly generated map) with some useful modifications and custom in-game notifications.

Server Requirements:
  • 4 GB RAM
  • 15 GB free space on /opt (for Alpha 18)
  • 2 CPU cores (2.4GHz+)
Reference: Linux Server Management Scripts

These paths will be used:

/opt/sdtd - home for the user running the server and base path for the server management script system
/bak/sdtd - by default backups of game data files will go here
/usr/local/bin/7dtd.sh - Management script entry point
/usr/local/lib/7dtd/ - Base path of script components
/etc/7dtd.conf - Global configuration options
/etc/bash_completion.d/7dtd - Bash-completion configuration to auto-complete the commands for the scripts
/etc/cron.d/7dtd-backup - Cron job for game backups
/etc/init.d/7dtd.sh - Init script to start/stop the instances on host boot/shutdown

NOTE: Make sure you have enough space on /opt to install 7 Days to Die which is about 12 GB for Alpha 18. You will also need additional space as players explore the map and expand the savegame size.

Install Ubuntu Server

This article assumes Ubuntu was installed following these instructions: How to Install Ubuntu Server

Firewall Rules

Edit the firewall script that was created during the initial setup of the server (if you followed my instructions):

Code: Select all

sudo vi /var/scripts/prod/en-firewall.sh
Add the following:

NOTE: The Telnet port is commented out since it should not be open to the entire Internet but left there as documentation about the port.

Code: Select all

echo "Adding 7dtd Server rules"
ufw allow proto tcp to any port 27000 comment '#1 game port' 1>/dev/null 2>&1
ufw allow proto udp to any port 27000:27002 comment '#1 Steam communication' 1>/dev/null 2>&1
ufw allow proto tcp to any port 8080 comment '#1 Web control panel' 1>/dev/null 2>&1
#ufw allow proto tcp to any port 8081 comment '#1 Telnet control panel' 1>/dev/null 2>&1
ufw allow proto tcp to any port 8082 comment '#1 Allocs Web Map' 1>/dev/null 2>&1

ufw allow proto tcp to any port 27003 comment '#2 game port' 1>/dev/null 2>&1
ufw allow proto udp to any port 27003:27005 comment '#2 Steam communication' 1>/dev/null 2>&1
ufw allow proto tcp to any port 8083 comment '#2 Web control panel' 1>/dev/null 2>&1
#ufw allow proto tcp to any port 8084 comment '#2 Telnet control panel' 1>/dev/null 2>&1
ufw allow proto tcp to any port 8085 comment '#2 Allocs Web Map' 1>/dev/null 2>&1
Run the updated rules:

Code: Select all

sudo /var/scripts/prod/en-firewall.sh
Install Prerequisites

Code: Select all

sudo apt install xmlstarlet gcc
Create Low-Rights User Accounts

Code: Select all

sudo addgroup sdtd
sudo addgroup steam
sudo useradd --comment "7 Days to Die Server" --shell /bin/bash --home /opt/sdtd --gid sdtd sdtd
sudo useradd --comment "Steam Command" --gid sudo --gid steam --create-home steam
Install Alloc's Server Management Scripts

Download the scripts archive, extract it, rename the home folder, set correct file ownership, re-archive and re-extract to the final destination while preserving the changes that were made:

Code: Select all

sudo mkdir /bak/sdtd
sudo chown sdtd:sdtd /bak/sdtd
mkdir /tmp/t
cd /tmp
wget http://illy.bz/fi/7dtd/management_scripts.tar.gz
tar --touch -xvzf /tmp/management_scripts.tar.gz -C /tmp/t/
mv /tmp/t/home /tmp/t/opt
sudo chown --recursive root:root /tmp/t/*
sudo chown --recursive sdtd:sdtd /tmp/t/opt/sdtd
sudo chmod 0755 /tmp/t/etc/init.d/7dtd.sh
sudo chmod 0755 /tmp/t/etc/bash_completion.d/7dtd
sudo chmod 0755 /tmp/t/usr/local/bin/7dtd.sh
sudo find /tmp/t/usr/local/lib/7dtd -type f -name '*.sh' -exec chmod 0755 {} \;
sudo tar -czvf /tmp/7dtd-new.tar.gz /tmp/t/*
sudo tar -xzvf /tmp/7dtd-new.tar.gz -C / --strip-components=2
Enable bash completion - This will allow you to press TAB after typing 7dtd.sh and seeing the options you can type after it.

Code: Select all

sudo vi /etc/bash.bashrc
Starting on line 35, uncomment these lines by removing the # character at the beginning of each line:

Code: Select all

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
Edit the configuration file and fix the paths:

Code: Select all

sudo vi /etc/7dtd.conf
Change line 19 from:

Code: Select all

export SDTD_BASE=/home/sdtd
to:

Code: Select all

export SDTD_BASE=/opt/sdtd
Change line 25 from:

Code: Select all

export SDTD_BACKUP_ROOT=/home/sdtd/backup
to:

Code: Select all

export SDTD_BACKUP_ROOT=/bak/sdtd
Install 7 Days to Die Server Files

Run this command to install SteamCMD and download the game engine. You can choose to install the experimental or stable version.

Code: Select all

7dtd.sh updateengine --experimental
or

Code: Select all

7dtd.sh updateengine --stable
Run this command to install Alloc's server fixes and web map service:

Code: Select all

7dtd.sh updatefixes
Run this command to make sure the update scripts are current:

Code: Select all

7dtd.sh updatescripts
Compile start-stop-daemon

Code: Select all

cd /usr/local/lib/7dtd/start-stop-daemon
gcc -o start-stop-daemon start-stop-daemon.c
sudo chown root:root start-stop-daemon
sudo chmod 0755 start-stop-daemon
Configure Default Admins

Make sure you know what your Steam 64 ID is and edit this file:

Code: Select all

sudo vi /opt/sdtd/templates/admins.xml
Around line 27, change this section from:

Code: Select all

    <admins>
        <!-- <admin steamID="STEAM GUID" permission_level="0" /> -->
    </admins>
to (using the example of a Steam 64 ID of 76561197970730192):

Code: Select all

    <admins>
        <admin steamID="76561197970730192" permission_level="0" />
    </admins>
Create a New Instance

Code: Select all

sudo 7dtd.sh instances create
Answer the setup questions how you want your server configured. Once created, we will then copy this configuration file to the templates folder and the next time we create another instance, the defaults will be how we configured this instance and we only need to change what is different from the first instance created.

If you called your instance "hamcraft" then this is how you would copy the config you just made to become the new default:

Code: Select all

sudo cp /opt/sdtd/instances/hamcraft/config.xml /opt/sdtd/templates/.
sudo chown sdtd:sdtd /opt/sdtd/templates/config.xml
sudo chmod 644 /opt/sdtd/templates/config.xml
Update Server
  1. List all game instances:

    Code: Select all

    sudo 7dtd.sh instances list

    Code: Select all

    Instance name        | Running  | Players | Port
    ---------------------+----------+---------+------
    hamcraft             |      yes |    1/ 8 | 27000
    navezgane            |      yes |    0/ 8 | 27003
  2. Stop each running instance:

    Code: Select all

    sudo 7dtd.sh kill hamcraft
    sudo 7dtd.sh kill navezgane
  3. Verify all instances have been shutdown:

    Code: Select all

    sudo 7dtd.sh instances list
  4. Make a backup of the savegames:

    Code: Select all

    sudo 7dtd.sh backup
  5. Update the game engine:
    if using stable branch:

    Code: Select all

    sudo 7dtd.sh updateengine --stable
    if using experimental branch:

    Code: Select all

    sudo 7dtd.sh updateengine --experimental
  6. Update the server fixes:

    Code: Select all

    sudo 7dtd.sh updatefixes
  7. Update the management scripts:

    Code: Select all

    sudo 7dtd.sh updatescripts
  8. Start the game instances that were running earlier:

    Code: Select all

    sudo 7dtd.sh start hamcraft
    sudo 7dtd.sh start navezgane
  9. Wait for the instances to load and check their log files for errors (look for "INF StartGame done" which means the server is ready to accept player connections):

    Code: Select all

    sudo vi /opt/sdtd/instances/hamcraft/logs/current_output_log.txt

    Code: Select all

    sudo vi /opt/sdtd/instances/navezgane/logs/current_output_log.txt
  10. On your gaming PC, start up the client and see if you can connect and verify all mods are working like normal.
Backup Data

To manually initiate a backup, run this command:

Code: Select all

sudo 7dtd.sh backup
To have it run at various times throughout the day, edit the root crontab:

Code: Select all

crontab -u root -e
Add this line:

Code: Select all

0 8,12,17,20,22 * * * /usr/local/bin/7dtd.sh backup > /dev/null 2>&1
Be sure to copy the savegames to a different location and keep a copy offsite (such as another computer or external hard drive or web service)

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Instance Creation Options

Post: # 779Post LHammonds
Tue Oct 15, 2019 1:47 pm

Instance Creation Options

Here are the options presented to you when creating a new instance. Be sure to give a unique instance name and port numbers.

Code: Select all

7dtd.sh instances create

Code: Select all

Instance name: navezgane
Server name: NavCraft
Server password:
Public server, options: 2: public
Base Port: 27003
Disabled network protocols, options: 1: None
Server description: Navezgane County
Server website URL: 7dtd.hammondslegacy.com
Hide command execution in log, options: 0: Log all
Max uncovered chunks per player: 131072
Max download speed for the world (KiB/s): 512
Limit allowed view distance settings of clients (5-12): 12
Enable EasyAntiCheat: true
Maximum number of concurrent zombies: 60
Maximum number of concurrent animals: 50
Max players: 4
Reserved VIP slots: 0
Permission level required for VIP slots: 100
Admin slots: 0
Permission level required for admin slots: 0
Enable control panel: false
Control panel port: 8083
Control panel password: supersecretpass
Telnet port: 8084
Telnet password: superdupersecretpass
Max failed login attempts: 10
Telnet login blocktime after failed logins (seconds): 10
World decoration seed: mombo#5
World name, options: 2: Navezgane
Random generation seed (if world RWG):
Random generation map size (if world RWG) (2048-16384):
Game mode, options: 1: GameModeSurvival
XP gain multiplier %: 100
Party range to share kill / quest XP rewards: 100
Player killing, options: 3: Kill everyone
Persistent player profiles: false
Safe zone up to player level: 5
Safe zone up to player hours: 5
Item spawn menu: false
Block damage modifier for players (%): 100
Block damage modifier for AIs (%): 100
Block damage modifier for AIs during blood moons (%): 100
Drop on Death, options: 2: Toolbelt only
Drop on Quit, options: 0: Nothing
Loot abundance (%): 100
Loot respawn delay (days): 7
Airdrop delay (hours): 72
Enable AirDrop markers: true
Length of one day: 60
Duration of daylight (in ingame hours) (0-24): 18
Difficulty (+ damage given / received), options: 2: Nomad (100% / 100%)
Zombie speed, regular, options: 0: Walk
Zombie speed, night, options: 2: Run
Zombie speed, ferals, options: 3: Sprint
Zombie speed, bloodmoons, options: 3: Sprint
Spawn mode: true
Enemy difficulty, options: 0: Normal
Enemies per Player on Blood moons (0-64): 8
Bedroll deadzone size: 15
Bedroll expiry time (days) 45
Land claim size: 13
Minimum keystone distance: 30
Claim expiry time (days): 3
Claim decay mode, options: 0: Linear
Claim durability modifier - online: 4
Claim durability modifier - offline: 4

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Alloc's Web Map

Post: # 780Post LHammonds
Tue Oct 15, 2019 4:05 pm

Alloc's Web Map

By default, the web map permissions do not allow the public to see the map. We will modify the permissions to make the map more usable.

Here are the goals of these changes:
  • Not logged in - Only allow the map to be visible to guests.
  • Logged in - Normal players can see their own location, land claim blocks, total players online.
  • Logged in - Admin players are not restricted and have access to all features.
Let's edit the 1st instance we create:

Code: Select all

vi /opt/sdtd/instances/hamcraft/webpermissions.xml
Here is the default settings:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<webpermissions>
        <admintokens>
                <!-- <token name="adminuser1" token="supersecrettoken" permission_level="0" /> -->
        </admintokens>
        <permissions>
                <!-- <permission module="web.map" permission_level="1000" /> -->
                <!-- <permission module="webapi.getlog" permission_level="0" /> -->
                <!-- <permission module="webapi.executeconsolecommand" permission_level="0" /> -->
                <!-- <permission module="webapi.getstats" permission_level="1000" /> -->
                <!-- <permission module="webapi.getplayersonline" permission_level="1000" /> -->
                <!-- <permission module="webapi.getplayerslocation" permission_level="1000" /> -->
                <!-- <permission module="webapi.viewallplayers" permission_level="1" /> -->
                <!-- <permission module="webapi.getlandclaims" permission_level="1000" /> -->
                <!-- <permission module="webapi.viewallclaims" permission_level="1" /> -->
                <!-- <permission module="webapi.getplayerinventory" permission_level="1" /> -->
                <!-- <permission module="webapi.gethostilelocation" permission_level="1" /> -->
                <!-- <permission module="webapi.getanimalslocation" permission_level="1" /> -->
        </permissions>
</webpermissions>
We will now change the permissions to look like the following:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<webpermissions>
        <admintokens>
                <token name="adminuser1" token="supersecrettoken" permission_level="0" />
        </admintokens>
        <permissions>
                <permission module="web.map" permission_level="2000" />
                <permission module="webapi.getlog" permission_level="0" />
                <permission module="webapi.executeconsolecommand" permission_level="0" />
                <permission module="webapi.getstats" permission_level="1000" />
                <permission module="webapi.getplayersonline" permission_level="1000" />
                <permission module="webapi.getplayerslocation" permission_level="1000" />
                <permission module="webapi.viewallplayers" permission_level="1" />
                <permission module="webapi.getlandclaims" permission_level="1000" />
                <permission module="webapi.viewallclaims" permission_level="1" />
                <permission module="webapi.getplayerinventory" permission_level="1" />
                <permission module="webapi.gethostilelocation" permission_level="1" />
                <permission module="webapi.getanimalslocation" permission_level="1" />
        </permissions>
</webpermissions>

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Restart Script

Post: # 787Post LHammonds
Sun Nov 10, 2019 11:18 am

Restart Script

The server instance can shutdown from time to time due to a crash or whatnot. This script is designed to check on the running status of a particular instance and start it if not running.

Create the script file and set appropriate permissions:

Code: Select all

sudo touch /var/scripts/prod/sdtd-crashfix.sh
sudo chown root:root /var/scripts/prod/sdtd-crashfix.sh
sudo chmod 700 /var/scripts/prod/sdtd-crashfix.sh
/var/scripts/prod/sdtd-crashfix.sh

Code: Select all

#!/bin/bash
#############################################################
## Name          : sdtd-crashfix.sh
## Version       : 1.0
## Date          : 2019-11-10
## Author        : LHammonds
## Purpose       : Check instance status and start if not running.
## Compatibility : Verified on Ubuntu Server 18.04 LTS
##                 7 Days to Die, Alpha 18
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2019-11-10 1.0 LTH Created script.
#############################################################
InstancePath="/opt/sdtd/instances"
DoNotStart="/tmp/sdtd-donotstart.txt"
ServerInstance=$1

## Check for override and make sure the instances are not stopped for a reason.
## We do not want to start an instance if something has it down on purpose.
if [[ -f ${DoNotStart} ]]; then
  /bin/echo "[WARN] Cannot start server. A 'DoNotStart' file detected."
  exit 0
fi

## Make sure a valid instance was passed to the script.
if [[ ! -f "${InstancePath}/${ServerInstance}/config.xml" ]];then
  /bin/echo "[ERROR] Cannot detect server instance called ${ServerInstance}"
  exit 1
fi

## Start instance if not running.
if [[ `/usr/local/bin/7dtd.sh status ${ServerInstance} | grep "Status: Running"` ]]; then
  /bin/echo "[INFO] ${ServerInstance} is running"
else
  /bin/echo "[INFO] ${ServerInstance} is stopped. Starting instance now..."
  /usr/local/bin/7dtd.sh start ${ServerInstance}
fi
exit 0
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here are the lines to run the script every 5 minutes for game instances called hamcraft and navezgane:

Code: Select all

*/5 * * * * /var/scripts/prod/sdtd-crashfix.sh hamcraft > /dev/null 2>&1
*/5 * * * * /var/scripts/prod/sdtd-crashfix.sh navezgane > /dev/null 2>&1

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Horde Notification Script

Post: # 789Post LHammonds
Tue Nov 26, 2019 5:07 pm

Horde Notification Script

This bash script will send out a notification to online players once per day in the morning as to when horde night will be arriving.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-horde-notify.sh
sudo chown root:root /var/scripts/prod/sdtd-horde-notify.sh
sudo chmod 700 /var/scripts/prod/sdtd-horde-notify.sh
/var/scripts/prod/sdtd-horde-notify.sh

Code: Select all

#!/bin/bash
#############################################################
## Name          : sdtd-horde-notify.sh
## Version       : 1.0
## Date          : 2017-05-08
## Author        : LHammonds
## Purpose       : Notify players once a day about approaching horde night.
##     1. Only process this script if there are players online.
##     2. Only process this script if the time is > 5:00 am in-game.
##     3. Finally, send chat message with how many days until horde night.
## Installation  : Run from crontab every few minutes.
## Compatibility : Verified on Ubuntu Server 14.04 thru 18.04 LTS
##                 7 Days to Die, Alpha 15.2 - Alpha 18.1
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2017-05-08 1.0 LTH Created script.
#############################################################
. /usr/local/lib/7dtd/common.sh
ServerInstance=$1
InstancePath="/opt/sdtd/instances"
TempFile="/tmp/sdtd-gettime-${ServerInstance}.txt"
LastRun="/tmp/sdtd-lastrun-${ServerInstance}.txt"
Hour2Notify=5

## Make sure a valid instance was passed to the script.
if [[ ! -f "${InstancePath}/${ServerInstance}/config.xml" ]];then
  /bin/echo "[ERROR] Cannot detect server instance called ${ServerInstance}"
  exit 1
fi

## Send ListPlayers command results to a temporary file.
telnetCommand ${ServerInstance} "lp" > ${TempFile}
## Get the last line of the file.  Example: Total of 13 in the game
PlayerCount=`/usr/bin/tail -n 1 ${TempFile}`
## Get the 3rd column of the line.  Example: 13
PlayerCount=`/bin/echo ${PlayerCount} | cut -d' ' -f3`
## Delete the temporary file.
/bin/rm ${TempFile}

if [[ ${PlayerCount} == 0 ]]; then
  ## No active players online.  No need to send in-game message.
  /bin/echo "[INFO] No players online, aborting script."
  exit 0
fi
## Send gettime command results to a temporary file.
telnetCommand ${ServerInstance} "gettime" > ${TempFile}

## Loop through the file looking for the line starting with Day.
while IFS= read -r line
do
  if [[ "${line}" == "Day "* ]]; then
    ## We found the date line.
    DayLine=${line}
    break
  fi
done < ${TempFile}
## Example of DayLine right now: Day 115, 05:22
## Get the 2nd column of the line.  Example: 115,
CurrentDay=`/bin/echo ${DayLine} | /usr/bin/cut -d' ' -f2`

## Remove the comma.  Example: 115
CurrentDay="${CurrentDay//,}"

## Get the in-game time.  Example: 05:22
CurrentTime=`/bin/echo ${DayLine} | /usr/bin/cut -d' ' -f3`

## Get the in-game hour.  Example: 05
CurrentHour=`/bin/echo ${CurrentTime} | /usr/bin/cut -d':' -f1`

## Force number to decimal (base 10) by removing any leading zeroes.  Example: 5
CurrentHour=$((10#${CurrentHour}))

## Get the remainder from dividing by 7.  Example: 3
Remainder=`/usr/bin/expr ${CurrentDay} % 7`

## Subtract the remainder from 7 to get how many days until the 7th night.
DaysToHorde=`/usr/bin/expr 7 - ${Remainder}`

if [[ ${CurrentHour} -gt ${Hour2Notify} ]]; then
  ## It is not too early in the day to send the notification.
  if [ -f ${LastRun} ]; then
    ## If last message sent was today, abort script.
    PriorDay=`/usr/bin/tail -n 1 ${LastRun}`
    if [[ ${PriorDay} == ${CurrentDay} ]]; then
      ## Message already sent today, abort script.
      exit 0
    else
      ## We are in a new day.
      if [[ ${Remainder} == 0 ]]; then
        telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]Horde night tonight!\""
      elif [[ ${Remainder} == 1 ]]; then
        telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]${DaysToHorde} day until horde night.\""
      else
        telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]${DaysToHorde} days until horde night.\""
      fi
      ## Create last run file with date when message was sent.
      /bin/echo ${CurrentDay} > ${LastRun}
    fi
  else
    ## Message not sent today, send the chat message.
    if [[ ${Remainder} == 0 ]]; then
      telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]Horde night tonight!\""
    elif [[ ${Remainder} == 1 ]]; then
      telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]${DaysToHorde} day until horde night.\""
    else
      telnetCommand ${ServerInstance} "say \"[ffff00][INFO] [00ff00]${DaysToHorde} days until horde night.\""
    fi
    ## Create last run file with date when message was sent.
    /bin/echo ${CurrentDay} > ${LastRun}
  fi
else
  ## Too early in the morning to message players, abort script.
  exit 0
fi
## Delete the temporary file.
/bin/rm ${TempFile}
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here are the lines to run the script every 5 minutes for game instances called hamcraft and navezgane:

Code: Select all

*/5 * * * * /var/scripts/prod/sdtd-horde-notify.sh hamcraft > /dev/null 2>&1
*/5 * * * * /var/scripts/prod/sdtd-horde-notify.sh navezgane > /dev/null 2>&1

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Day Count Script for Web Page

Post: # 790Post LHammonds
Tue Nov 26, 2019 5:24 pm

Day Count Script for Web Page

This bash script will update a web page to show the current day counts for each instance you have. The web page should be pulled into your main web page via an include statment because the script re-creates the page each time.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-web-day.sh
sudo chown root:root /var/scripts/prod/sdtd-web-day.sh
sudo chmod 700 /var/scripts/prod/sdtd-web-day.sh
Make sure you modify where the "WebFile" variable to point to your website.
Make sure you modify the list of instances it runs against at the bottom of the script.

/var/scripts/prod/sdtd-web-day.sh

Code: Select all

#!/bin/bash
#############################################################
## Name          : sdtd-web-day.sh
## Version       : 1.0
## Date          : 2019-10-29
## Author        : LHammonds
## Purpose       : Output the current day for each map to a web file.
## Compatibility : Verified on Ubuntu Server 18.04 LTS
##                 7 Days to Die, Alpha 18
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2019-10-29 1.0 LTH Created script.
#############################################################
. /usr/local/lib/7dtd/common.sh
TempFile="/tmp/sdtd-web-gettime.txt"
WebFile="/var/www/html/inc-day.php"

f_getday () {
  ## Send gettime command results to a temporary file.
  telnetCommand $1 "gettime" > ${TempFile}
  ## Loop through the file looking for the line starting with Day.
  while IFS= read -r line
  do
    if [[ "${line}" == "Day "* ]]; then
      ## We found the date line.
      DayLine=${line}
      break
    fi
  done < ${TempFile}
  ## Example of DayVar right now: Day 115, 05:22
  ## Get the 2nd column of the line.  Example: 115,
  CurrentDay=$(echo ${DayLine} | cut -d' ' -f2)
  ## Remove the comma.  Example: 115
  CurrentDay="${CurrentDay//,}"
  ## Delete the temporary file.
  /bin/rm ${TempFile}
  /bin/echo "${CurrentDay}"
} ## f_getday()

if [[ -f ${WebFile} ]]; then
  /bin/rm ${WebFile}
fi
/usr/bin/touch ${WebFile}
/bin/chown www-data:www-data ${WebFile}
/bin/chmod 644 ${WebFile}

/bin/echo "<p>Day counts as of `date +%Y-%m-%d`:</p>" >> ${WebFile}
/bin/echo "<ul>" >> ${WebFile}
/bin/echo "<li>Hamcraft = $(f_getday 'hamcraft')</li>" >> ${WebFile}
/bin/echo "<li>Navezgane = $(f_getday 'navezgane')</li>" >> ${WebFile}
/bin/echo "</ul>" >> ${WebFile}
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here is how to run the script every hour:

Code: Select all

0 */1 * * * /var/scripts/prod/sdtd-web-day.sh > /dev/null 2>&1
If your server runs PHP and has file includes enabled, here is an example to pull in the day counts into the main index.php file:

Code: Select all

<html>
<body>
<?php include("inc-day.php");?>
</body>
</html>
Here is an example output on the web page:

Code: Select all

Day counts as of 2019-11-26:

 * Hamcraft = 293
 * Navezgane = 69

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

World Size Script for Web Page

Post: # 791Post LHammonds
Tue Nov 26, 2019 5:31 pm

World Size Script for Web Page

This bash script will update a web page to show the current world size for each instance you have. The web page should be pulled into your main web page via an include statment because the script re-creates the page each time.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-web-worldsize.sh
sudo chown root:root /var/scripts/prod/sdtd-web-worldsize.sh
sudo chmod 700 /var/scripts/prod/sdtd-web-worldsize.sh
Make sure you modify where the "WebFile" variable to point to your website.
Make sure you modify the list of instances it runs against at the bottom of the script as well as their file path location.

/var/scripts/prod/sdtd-web-worldsize.sh

Code: Select all

#!/bin/bash
#############################################################
## Name          : sdtd-web-worldsize.sh
## Version       : 1.0
## Date          : 2019-10-29
## Author        : LHammonds
## Purpose       : Output the current size for each map to a web file.
## Compatibility : Verified on Ubuntu Server 18.04 LTS
##                 7 Days to Die, Alpha 18
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2019-10-29 1.0 LTH Created script.
#############################################################
WebFile="/var/www/www/inc-worldsize.php"

if [[ -f ${WebFile} ]]; then
  /bin/rm ${WebFile}
fi
/usr/bin/touch ${WebFile}
/bin/chown www-data:www-data ${WebFile}
/bin/chmod 644 ${WebFile}

/bin/echo "<p>World map sizes as of `date +%Y-%m-%d`:</p>" >> ${WebFile}
/bin/echo "<ul>" >> ${WebFile}
/bin/echo "<li>Hamcraft = `/usr/bin/du -sh /opt/sdtd/instances/hamcraft | /usr/bin/awk '{print $1}'`</li>" >> ${WebFile}
/bin/echo "<li>Navezgane = `/usr/bin/du -sh /opt/sdtd/instances/navezgane | /usr/bin/awk '{print $1}'`</li>" >> ${WebFile}
/bin/echo "</ul>" >> ${WebFile}
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here is how to run the script every day at 5am:

Code: Select all

0 5 * * * /var/scripts/prod/sdtd-web-worldsize.sh > /dev/null 2>&1
If your server runs PHP and has file includes enabled, here is an example to pull in the world size into the main index.php file:

Code: Select all

<html>
<body>
<?php include("inc-worldsize.php");?>
</body>
</html>
Here is an example output on the web page:

Code: Select all

World map sizes as of 2019-11-26:

 * Hamcraft = 935M
 * Navezgane = 69M

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Backup Script

Post: # 792Post LHammonds
Tue Nov 26, 2019 5:37 pm

Backup Script

This bash script will create a full backup of your maps and notify players in-game when the backup starts and finishes.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-backup.sh
sudo chown root:root /var/scripts/prod/sdtd-backup.sh
sudo chmod 700 /var/scripts/prod/sdtd-backup.sh
Make sure you modify the list of instances it runs against at the bottom of the script as well as their file path location.

/var/scripts/prod/sdtd-backup.sh

Code: Select all

#!/bin/bash
#############################################################
## Name          : sdtd-backup.sh
## Version       : 1.0
## Date          : 2019-10-17
## Author        : LHammonds
## Purpose       : Backup all instances and notify players of the backup.
## Installation  : Run from crontab how many times you want a backup per day.
## Compatibility : Verified on Ubuntu Server 18.04.3 LTS
##                 7 Days to Die, Alpha 18
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2019-10-17 1.0 LTH Created script.
#############################################################
. /usr/local/lib/7dtd/common.sh

LogFile="/var/log/sdtd-backup.log"

/bin/echo "`date +%Y-%m-%d_%H:%M:%S` - [INFO] Backup started." | tee -a ${LogFile}
telnetCommand hamcraft "say \"[ffff00][INFO] [00ff00]Backup started.\""
telnetCommand navezgane "say \"[ffff00][INFO] [00ff00]Backup started.\""
/usr/local/bin/7dtd.sh backup
telnetCommand hamcraft "say \"[ffff00][INFO] [00ff00]Backup finished.\""
telnetCommand navezgane "say \"[ffff00][INFO] [00ff00]Backup finished.\""
/bin/echo "`date +%Y-%m-%d_%H:%M:%S` - [INFO] Backup completed." | tee -a ${LogFile}
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here is how to run the script at various hours throughout the day:

Code: Select all

0 8,12,17,20,22 * * * /var/scripts/prod/sdtd-backup.sh > /dev/null 2>&1

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Shutdown Script

Post: # 793Post LHammonds
Tue Nov 26, 2019 5:51 pm

Shutdown Script

This bash script will notify online players of a pending shutdown and then proceed to shutdown each instance.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-shutdown.sh
sudo chown root:root /var/scripts/prod/sdtd-shutdown.sh
sudo chmod 700 /var/scripts/prod/sdtd-shutdown.sh
Make sure you modify the list of instances it runs against at the bottom of the script as well as their file path location.

/var/scripts/prod/sdtd-shutdown.sh

Code: Select all

#!/bin/bash
#############################################
## Name          : sdtd-shutdown.sh
## Version       : 1.0
## Date          : 2016-01-18
## Author        : LHammonds
## Purpose       : Warn players and shutdown game.
## Compatibility : Verified on Ubuntu Server 14.04 thru 18.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/sdtd-shutdown.log"

## Requirement Check: Script must run as root user.
if [ "$(id -u)" != "0" ]; then
  ## FATAL ERROR DETECTED: Document problem and terminate script.
  /bin/echo -e "\nERROR: Root user required to run this script.\n"
  /bin/echo -e "Type 'sudo su' to temporarily become root user.\n"
  exit
fi

## Check parameter.
if [ "${1}" == "" ]; then
  /bin/echo -e "[ERROR] Missing required parameter. Enter the server instance."
  /bin/echo -e "Syntax: ${0} [ServerInstance]"
  /bin/echo -e "Example 1: ${0} hamcraft"
  /bin/echo -e "Example 2: ${0} navezgane"
  /bin/echo -e "Here is a list of valid instances:"
  /usr/local/bin/7dtd.sh instances list
  exit 1
else
  InstanceName="${1}"
fi

telnetCommand ${InstanceName} "say \"Server shutdown in 1 minute\""
/bin/sleep 30
telnetCommand ${InstanceName} "say \"Server shutdown in 30 seconds\""
/bin/sleep 20
telnetCommand ${InstanceName} "say \"Server shutdown in 10 seconds\""
/bin/sleep 7
telnetCommand ${InstanceName} "say \"Server shutdown in 3 seconds\""
/bin/sleep 1
telnetCommand ${InstanceName} "say \"Server shutdown in 2 seconds\""
/bin/sleep 1
telnetCommand ${InstanceName} "say \"Server shutdown in 1 second\""
/bin/sleep 1
telnetCommand ${InstanceName} "say \"Be right back!\""
/bin/sleep 1
telnetCommand ${InstanceName} "kickall Reboot"
/bin/sleep 1
telnetCommand ${InstanceName} "saveworld"
/bin/sleep 1
telnetCommand ${InstanceName} "shutdown"
/bin/echo "`date +%Y-%m-%d_%H:%M:%S` - [INFO] Shutting down ${InstanceName} instance." | tee -a ${LogFile}
If you followed my server setup instructions, then you should probably include this shutdown script in the "servicestop.sh" script.

Be sure to substitute your instance names instead of the examples provided below:

/var/scripts/prod/servicestop.sh

Code: Select all

/var/scripts/prod/sdtd-shutdown.sh navezgane
/var/scripts/prod/sdtd-shutdown.sh hamcraft
Also make sure your "servicestart.sh" script includes the commands to start your instances as well:

/var/scripts/prod/servicestart.sh

Code: Select all

/usr/local/bin/7dtd.sh start hamcraft
/usr/local/bin/7dtd.sh start navezgane

User avatar
LHammonds
Site Admin
Site Admin
Posts: 779
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Player List Script for Web Page

Post: # 794Post LHammonds
Tue Nov 26, 2019 6:11 pm

Player List Script for Web Page

This bash script will update a web page for each instance to show all known players. The web page(s) should be pulled into your main web page via an include statment because the script re-creates the page each time.

Create the file to hold the script:

Code: Select all

sudo touch /var/scripts/prod/sdtd-web-players.sh
sudo chown root:root /var/scripts/prod/sdtd-web-players.sh
sudo chmod 700 /var/scripts/prod/sdtd-web-players.sh
Make sure you modify where the "WebPrefix" variable to point to your website.
Make sure you modify the list of instances it runs against at the bottom of the script.

/var/scripts/prod/sdtd-web-players.sh

Code: Select all

#!/bin/bash
#!/bin/bash
#############################################################
## Name          : sdtd-web-players.sh
## Version       : 1.0
## Date          : 2019-11-26
## Author        : LHammonds
## Purpose       : Display player info to a web file.
## Compatibility : Verified on Ubuntu Server 18.04 LTS
##                 7 Days to Die, Alpha 18
###################### CHANGE LOG ###########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2019-11-26 1.0 LTH Created script.
#############################################################
. /usr/local/lib/7dtd/common.sh
TempFile1="/tmp/sdtd-web-players.txt"
TempFile2="/tmp/sdtd-web-players2.txt"
WebPrefix="/var/www/www/inc-players"

f_getplayers () {
  WebFile="${WebPrefix}-${1}.php"
  ## Prep destination file.
  if [[ -f ${WebFile} ]]; then
    /bin/rm ${WebFile}
  fi
  if [[ -f ${TempFile1} ]]; then
    /bin/rm ${TempFile1}
  fi
  if [[ -f ${TempFile2} ]]; then
    /bin/rm ${TempFile2}
  fi
  /usr/bin/touch ${WebFile}
  /bin/chown www-data:www-data ${WebFile}
  /bin/chmod 644 ${WebFile}
  /usr/bin/touch ${TempFile1}
  /bin/chown www-data:www-data ${TempFile1}
  /bin/chmod 644 ${TempFile1}
  /usr/bin/touch ${TempFile2}
  /bin/chown www-data:www-data ${TempFile2}
  /bin/chmod 644 ${TempFile2}
  /bin/echo "<p>Players in ${1}:</p>" >> ${WebFile}
  /bin/echo "<table>" >> ${WebFile}
  /bin/echo "<tr><th>Playername</th><th>Minutes played</th><th>Last seen</th></tr>" >> ${WebFile}
  ## Send gettime command results to a temporary file.
  telnetCommand $1 "lkp" > ${TempFile1}
  ## Perform magic on file.
  PlayersFound="0"
  while IFS= read -r line
  do
    if [[ "${line}" == *"1. "* ]]; then
      ## We found the 1st line of player data.
      PlayersFound="1"
    fi
    if [[ "${line}" == *"Total of "* ]]; then
      ## We have all player data at this point.
      PlayersFound="0"
    fi
    if [[ "${PlayersFound}" == "1" ]]; then
      /bin/echo ${line} >> ${TempFile2}
    fi
  done < ${TempFile1}
  ## Example line being read:
  ## 1. Jon Doe, id=171, steamid=11112222333344445, online=False, ip=166.173.59.64, playtime=12 m, seen=2019-10-15 18:25
  while IFS= read -r line
  do
    PlayerName=$(/bin/echo ${line} | /usr/bin/cut -f1 -d',')
    SteamID=$(/bin/echo ${line} | /usr/bin/cut -f3 -d',')
    PlayTime=$(/bin/echo ${line} | /usr/bin/cut -f6 -d',')
    LastSeen=$(/bin/echo ${line} | /usr/bin/cut -f7 -d',')
    ## Example of variables during 1st pass:
    ## 1. Jon Doe, steamid=11112222333344445, playtime=12 m, seen=2019-10-15 18:25
    PlayerName=$(/bin/echo ${PlayerName} | /usr/bin/cut -f2 -d'.')
    SteamID=$(/bin/echo ${SteamID} | /usr/bin/cut -f2 -d'=')
    PlayTime=$(/bin/echo ${PlayTime} | /usr/bin/cut -f2 -d'=')
    LastSeen=$(/bin/echo ${LastSeen} | /usr/bin/cut -f2 -d'=')
    ## Example of variables during 2nd pass:
    ## Jon Doe, 11112222333344445 12 m 2019-10-15 18:25
    PlayerName=$(/bin/echo ${PlayerName} | /usr/bin/cut -f1 -d',')
    PlayTime=$(/bin/echo ${PlayTime} | /usr/bin/cut -f1 -d' ')
    LastSeen=$(/bin/echo ${LastSeen} | /usr/bin/cut -f1 -d' ')
    ## Example of variables during 3rd pass:
    ## Jon Doe 11112222333344445 12 2019-10-15 18:25
    echo "<tr><td><a href='http://steamcommunity.com/profiles/${SteamID}'>${PlayerName}</a></td><td>${PlayTime}</td><td>${LastSeen}</td></tr>" >> ${WebFile}
  done < ${TempFile2}
  /bin/echo "</table>" >> ${WebFile}
} ## f_getplayers()

f_getplayers 'hamcraft'
f_getplayers 'navezgane'
Edit your root crontab schedule:

Code: Select all

sudo crontab -u root -e
Here is how to run the script day at 6am:

Code: Select all

0 6 * * * /var/scripts/prod/sdtd-web-players.sh > /dev/null 2>&1
If your server runs PHP and has file includes enabled, here is an example to pull this script into the main index.php file (be sure to match the names of the instances to your instances):

Code: Select all

<html>
<body>
<?php include("inc-players-hamcraft.php");?>
<?php include("inc-players-navezgane.php");?>
</body>
</html>
Here is an example output on the web page:

Code: Select all

Players in hamcraft:

Playername	Minutes played	Last seen
Jon Doe		12		2019-10-15
Jane Doe	0		2019-10-18
If you want to make each column sortable, you can grab the JavaScript from kyrogenix.org. Just make a few alterations needed and it will be sortable in no time.

Post Reply