With everything in place let’s automate the server so we can be lazy and not have to start it manually every time.
- Part 1 – Basics and setting up a vanilla server
- Part 2 – Adding mods to your server
- Part 3 – Automating the server <– you are here
- Part 4 – Getting a custom url for your server
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
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/**/*.*"
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
Description=SRB2Kart Server Logger
ExecStart=/bin/bash -c "/usr/bin/tail -f /home/srb2kart/.srb2kart/log.txt | systemd-cat --identifier=srb2kartlog"
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 service srb2kart enable
sudo service srb2kartlogger enable
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 12. 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.
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.
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:
Now you should have everything ready and automated. If you have any issues feel free to @me (himie) on our kart.moe discord server or the srb2kart discord.
Thanks to Ashnal for the automated configuration of the services above!