SRB2Kart Dedicated Server Hosting – Part 3

With everything in place let’s automate the server so we can be lazy and not have to start it manually every time.

Index

  1. Part 1 – Basics and setting up a vanilla server 
  2. Part 2 – Adding mods to your server
  3. Part 3 – Automating the server <– you are here
  4. Part 4 – Getting a custom url for your server

SRB2Kart Services

Most linux distros these days, including Ubuntu, run on systemd which handles all the services in the system so we’ll add a couple to it.

The first service we’ll create will be to keep a virtual terminal open in which the game will run. If the game crashes for any reason the service will automatically restart the server so there will be very little downtime if it goes down for any reason.

To run the virtual terminal byobu is a good option so install that first

sudo apt-get install byobu

First create the srb2kart.service file and edit it. Note that you have to use sudo which means you’ll have to enter your password after typing the command.

sudo nano /etc/systemd/system/srb2kart.service

and paste the following in it

[Unit]
Description=SRB2Kart Server
After=network.target
StartLimitIntervalSec=0
Wants=srb2kartlogger.service
[Service]
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
PermissionsStartOnly=false
WorkingDirectory=/home/srb2kart/.srb2kart
Environment=LC_ALL=C
ExecStartPre=-/home/srb2kart/.srb2kart/update.sh
ExecStart=/usr/bin/byobu new-session -ds srb2kart /bin/bash -c "shopt -s globstar && shopt -s nocaseglob && shopt -s nullglob && /usr/games/srb2kart -consisdump -dedicated -file mods/loadfirst/**/*.* bonuschars.kart mods/chars/**/*.* mods/tracks/**/*.* mods/loadlast/**/*.*"
User=srb2kart
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
Type=forking
RemainAfterExit=false
Restart=always
RestartSec=1s
TimeoutSec=180
[Install]
WantedBy=multi-user.target

Save the file and exit

Repeat the same for a logger service which will keep a log of the server and see if anything wrong is happening.

sudo nano /etc/systemd/system/srb2kartlogger.service

and paste this

[Unit]
Description=SRB2Kart Server Logger
Before=srb2kart.service
StartLimitIntervalSec=0
[Service]
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
WorkingDirectory=/home/srb2kart/.srb2kart
ExecStart=/bin/bash -c "/usr/bin/tail -f /home/srb2kart/.srb2kart/log.txt | systemd-cat --identifier=srb2kartlog"
User=srb2kart
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
Type=simple
RemainAfterExit=false
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target

Save the file and exit

Now we can enable the service and the server should start up

sudo service srb2kart start

sudo service srb2kartlogger start

And enable them so it starts automatically when the server boots up

sudo systemctl enable srb2kart

sudo systemctl enable srb2kartlogger

Now you should be able to login to your server and play! The server will automatically restart if anything goes wrong so you don’t have to worry about it.

However, sometimes when the server runs for too long anyone trying to connect will get stuck at loading server state. The only solution we’ve found for this is to regularly restarting the server every day. Next let’s setup a cronjob that will restart the server automatically every day.

Automatically restart server with cronjob

To restart the server automatically every day to avoid clients getting stuck we can use the cronjob. The crontab has specifically formatted lines to run commands on a schedule. In my server I have it set to restart the server every day at 3am after sending a couple messages to anyone connected warning them that the server is about to restart.

To add these scheduled jobs do the following.

Edit the system crontab:

sudo crontab -e

This file has comments explaining how the crontab works and how it is formatted. In our case we’ll add the followings line:

00 03 * * * systemctl restart srb2kart

This line will restart the server every day at 3:00 am. If you want to change the time that it restarts it’s pretty simple. The first two numbers indicate the time in minutes, the second set of numbers indicate the hour. So 00 03 means 03:00. If you wanted to change to 12:00 am the first 4 set of numbers would be 00 00. The asterisks indicate that the command should be run every day of the month, every month, every day of the week. You can read up more on crontab if you wish to customize this further.

Do the same but without sudo. This will add commands to warn users about the server restarting and it has to run under your srb2kart user instead of the system.

crontab -e

and paste these lines

55 02 * * * byobu send-keys -t srb2kart "csay daily server restart\in 5 minutes" ENTER
59 02 * * * byobu send-keys -t srb2kart "csay daily server restart\in 1 minute" ENTER

The first line will run every day at 2:55 am warning anyone connected that the server will restart in 5 minutes. Second line will do the same but at 2:59 am, 1 minute before it restarts.

Important to note here that most servers are configured with a UTC timezone by default. You can either set the times in the crontab as UTC or look up how to use timedatectl to change the timezone of the server.

You can check the logging service at any time by typing the following command:

journalctl -eu srb2kartlogger -n30000

It will show all the lines that the log has including lines from a previous session so you can go back and check if something went wrong.

If you want to interact with the srb2kart server console you need to attach to the byobu virtual console:

byobu a

Credits

Thanks to Ashnal for the automated configuration of the services above!

This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *