Welcome to AC Web.
Results 1 to 5 of 5
  1. #1

    [How To] Use the Linux kernel as a restarter [Linux/systemd]


    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    There are many out there running their WoW servers on Linux. Since they don't want to have to log into the Linux box to restart the server if it goes down, they'll typically use some kind of restarter script to acheive this goal. Some of these methods can get a little complex and may involve cron jobs (periodically run jobs) and other methods. But why? Isn't checking and running programs the kernel's job in the first place? Why can't it do this? Well it can and I'm here to show you.

    Effectively, this guide is about making your WoW server run as a Linux service. To achieve this we'll be relying on systemd which is used in pretty much all modern Linux distros. Getting this guide to work with older init.d is beyond the scope of this guide.

    The benefits of doing this is that the you won't need some other restarter script and since the Linux kernel itself will be monitoring things, it can also start the server on system boot. The one drawback is that stopping the server will require a different shutdown process (don't worry, it isn't that weird).

    The real issue with trying to get the server to run as a service is two-fold. First, systemd is a kernel module and so care must be taken to run the server as a specific user. Second, the WoW server has a command console and we'll likely also want to run the server under screen, tmux, dtach or other such terminal multiplexer so we'll need to pay close attention to how we get the server running in a proper terminal.

    Systemd uses service files. These are basically .ini like files that describe what systemd should do with a unit (a unit is just systemd's word for a program or process). I'm not going to cover all the little details of these unit files or we'd be here all week. There is plenty of information on the web. I will however mention what's used in the service file shown here and why it is there.

    The example files shown here are for a TrinityCore WotLK server, though little would need to be changed for a MaNGOS based server. Anyway, lets look at the service file for starting the authentication server.

    authserver.service
    Code:
    [Unit]
    Description=the authentication server
    Requires=mysqld.service
    After=mysqld.service getty.service network.service
    
    [Service]
    # What should be run (adjust paths as needed)
    Type=forking
    WorkingDirectory=/home/deb/core/bin
    ExecStart=/home/deb/core/service/startauth
    
    # Run the server as this user and group
    User=deb
    Group=deb
    
    # How and when to restart
    Restart=always
    RestartSec=5s
    
    # Setup some inital terminal environment to run under
    Environment=TERM=linux
    StandardInput=tty
    StandardOutput=tty
    TTYPath=/dev/tty20
    
    [Install]
    WantedBy=multi-user.target
    The [Unit] section basically just gives a quick description of the service and when it should be loaded. The Requires line here tells systemd that if the mysql server isn't running, fail this service as well (it is kinda hard to run a WoW server without the database now isn't it). We also tell systemd that it should load the authserver after mysql, the network and the terminal are up and running. Note that when specifying paths in systemd service files, you should specify absolute paths, not relative ones.

    The [Install] section at the end tells systemd that if we enable this unit to be run on bootup, it should be run as part of the normal multi-user bootup process.

    The [Service] section is the most important as it tells systemd how to run this unit. The first three lines of this section tell systemd that we'll be forking the main processs. This is because we'll run the actual server in a terminal multiplexer such as screen, tmux, or dtach so this helps systemd figure out what the main process ID will be. The third line is just the full path and filename of the script to be run.

    Since we want to run the WoW server as a specific user as opposed to running it as root (which is a bad idea) we now specify the username and groupname for the user we want to be run as.

    The next two lines tell systemd that if the process (i.e. the WoW server) exits for whatever reason, we want it to be restarted, but wait 5 seconds before actually doing so.

    The final 4 lines of this section set things up so that the script will actually have an environment to run in. It won't be perfect but the remaining setup can be done in the script itself. This just gets us a terminal device to run under (specifically tty20 in this case).

    So anyway, save the above file in /etc/systemd/system (you need to have root privileges to do so).

    Now for the script that the above service file will call.
    startauth
    Code:
    #!/bin/bash
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    . /etc/environment
    export LANGUAGE
    export LANG
    export LC_ALL
    
    dtach -n /tmp/auth ./authserver
    This script is rather simple. Mostly it sets up a default environment then runs the authserver. Here I'm using dtach, though you could use screen, tmux or other terminal multiplexer of your choice.

    Save this file somewhere, set it's execution permission (do chmod +x startauth) and make sure that the user that will run the server (as set in the service file) has permission to run this file and also make sure that the path and filename match in the service file itself.

    So that's it for the authserver. The worldserver is almost identical, so I'll just show them without any more comments.

    worldserver.service
    Code:
    [Unit]
    Description=the world server
    Requires=mysqld.service
    After=mysqld.service getty.service network.service
    
    
    [Service]
    # What should be run
    Type=forking
    WorkingDirectory=/home/deb/core/bin
    ExecStart=/home/deb/core/service/startworld
    
    # Run the server as this user and group
    User=deb
    Group=deb
    
    # How and when to restart
    Restart=always
    RestartSec=5s
    
    # Setup some inital terminal environment to run under
    Environment=TERM=linux
    StandardInput=tty
    StandardOutput=tty
    TTYPath=/dev/tty20
    
    
    [Install]
    WantedBy=multi-user.target
    startworld
    Code:
    #!/bin/bash
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    . /etc/environment
    export LANGUAGE
    export LANG
    export LC_ALL
    
    dtach -n /tmp/world ./worldserver
    Once the above is done, we need to tell systemd that some unit files have changed with
    Code:
    sudo systemctl daemon-reload
    (actually you only need to do this is you modify a service file, but hey...)

    To allow systemd to run the server on boot: (the following does both the authserver and worldserver)
    Code:
    sudo systemctl enable authserver worldserver
    To start the authserver and worldserver right now: (just use authserver or worldserver to start only one of them)
    Code:
    sudo systemctl start authserver worldserver
    to stop them do (again this does both at the same time. Use only one service name to stop an individual one)
    Code:
    sudo systemctl stop authserver worldserver
    That's it, once enabled and started, systemd itself will ensure the server keeps running.

    Oh yeah, dtach is just the terminal detaching code from screen that someone reworked into a light-weight terminal multiplexer. If you want to try it out as shown in this how-to, you can attach to the worldserver session with
    Code:
    dtach -a /tmp/world
    You can detach from the session by pressing Ctrl+\
    Last edited by MyLilSuccy; 02-04-2019 at 12:22 PM. Reason: Fixed a few typos

  2. #2
    Nice. thanks for the share

  3. #3
    Great guide, good job.

  4. #4


    Join Date
    Nov 2015
    Location
    Library of Elchea
    Posts
    682
    Amazing!

  5. #5

    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    Nice guide, Thanks sharing and explaining clearly each step of the process.

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •