Warning

This document is for Red's development version, which can be significantly different from previous releases. If you're a regular user, you should read the Red documentation for the current stable release.

Setting up auto-restart using systemd on Linux

Note

This guide is for setting up systemd on a Linux environment. This guide assumes that you already have a working Red instance.

Creating the service file

In order to create the service file, you will first need to know two things, your Linux username and your Python path

First, your Linux username can be fetched with the following command:

whoami

Next, your python path can be fetched with the following commands:

# If redbot is installed in a venv
source ~/redenv/bin/activate
/usr/bin/which python

# If redbot is installed in a pyenv virtualenv
pyenv shell <virtualenv_name>
pyenv which python

Then create the new service file:

sudo nano /etc/systemd/system/red@.service

Paste the following in the file, and replace all instances of username with the Linux username you retrieved above, and path with the python path you retrieved above.

[Unit]
Description=%I redbot
After=multi-user.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=path -O -m redbot %I --no-prompt
User=username
Group=username
Type=idle
Restart=on-abnormal
RestartSec=15
RestartForceExitStatus=1
RestartForceExitStatus=26
TimeoutStopSec=10

[Install]
WantedBy=multi-user.target

Save and exit ctrl + O; enter; ctrl + x

Starting and enabling the service

Note

This same file can be used to start as many instances of the bot as you wish, without creating more service files, just start and enable more services and add any bot instance name after the @

To start the bot, run the service and add the instance name after the @:

sudo systemctl start red@instancename

To set the bot to start on boot, you must enable the service, again adding the instance name after the @:

sudo systemctl enable red@instancename

If you need to shutdown the bot, you can use the [p]shutdown command or type the following command in the terminal, still by adding the instance name after the @:

sudo systemctl stop red@instancename

Warning

If the service doesn’t stop in the next 10 seconds, the process is killed. Check your logs to know the cause of the error that prevents the shutdown.

To set the bot to not start on boot anymore, you must disable the service by running the following command, adding the instance name after the @:

sudo systemctl disable red@instancename

You can access Red’s log through journalctl:

sudo journalctl -eu red@instancename