Discord bot on Raspberry PI as service

How To Run Discord Bot on Raspberry PI in the Background

In this post, we’ll take a look at how to run a Discord bot on Raspberry PI as a service. To explain, by doing so, we can continue to use the terminal without disturbing the running script. In other words, we run it in the background. Furthermore, this tutorial is only relevant if you’re running Linux OS on you Raspberry PI.

Additionally, we can start, stop, and check status of the running service by using systemctl command. In this particular case, we’re going to run a reaction role Discord bot from another post.

Defining Discord bot service on Raspberry PI

First of all, we’ll need to get to the system directory, which is in “/lib/systemd/system“. If you type ls command, you’ll see a whole bunch of .service files. Furthermore, our goal here is to create a custom .service file, which will run our Discord bot python script.

In order to do so, we’ll need to use the nano text editor. Following 2 commands will change current directory to system directory, and create and open the custom service file. In addition, you can name this .service file whatever you like, as long it doesn’t already exists in the directory.

cd /lib/systemd/system/
sudo nano discord_bot.service

Okay, now you should have the text editor open in terminal. This is where we’ll input all the necessary information for our service.

[Unit]
Description=Discord bot for managing roles
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/discord_bot.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

When you finish setting the information inside the file you can save it by pressing CTRL + O key combination and to confirm press ENTER. After that, we can exit the text editor by pressing CTRL + X.

In the .service file above, we created a simple service that runs the Discord bot python script and if it aborts for any reason, it’ll restart automatically. Furthermore, you can check more service’s options on Basic systemctl usage wiki site.

Finally, now that we have everything set for our service, we need to activate it. We can do so with the following commands.

sudo chmod 644 /lib/systemd/system/discord_bot.service
chmod +x /home/pi/discord_bot.py
sudo systemctl daemon-reload
sudo systemctl enable discord_bot.service
sudo systemctl start discord_bot.service

That’s it, your Discord bot should now be running in the background as a service.

NOTE: in case that you’re getting an error saying there’s no “discord” module, even though you have it installed, try reinstalling it with sudo command.

sudo python3 -m pip install -U "discord.py[voice]"

Service tasks

You do need to keep in mind that for every change that you make in the system directory, you’ll need to use the following command.

sudo systemctl daemon-reload

Here are also a few commands you’ll probably find useful when working with your Discord bot.

  • Check status:
sudo systemctl status discord_bot.service
  • Start service:
sudo systemctl start discord_bot.service
  • Stop service:
sudo systemctl stop discord_bot.service
  • Check service’s log:
sudo journalctl -f -u discord_bot.service

Conclusion

To conclude, we made a simple service, which will run a Discord bot in the background on Raspberry PI. I learned a lot while working on this project and I hope it proves helpful to you as well.

Share this article:

Related posts

Discussion(0)