Question

always keep active the database managed by my discord bot (discord.py)

Posted June 15, 2021 179 views
PythonDatabases

I have noticed that when I do after days my bot commands that force my database activation failed it does not connect until I restart the bot. I would like to have the bot always active without reboots with the managed database always connected. how can I do?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

There are a few ways to do that, the one that I would recommend is to use a service like

  • First update your APT repositories:
sudo apt update
  • Then install supervisor:
sudo apt install supervisor
  • After that create a new configuration file:
sudo nano /etc/supervisor/conf.d/discord.conf
  • And add the following content:
[program:yourproject]
directory=/home/your_username/bot/
command=/usr/bin/python /home/your_username/bot/discord.py
user=root
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/discord.err.log
stdout_logfile=/var/log/discord.out.log

For more information about supervisord make sure to check out this tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps

Hope that this helps!
Regards,
Bobby

Here's our tutorial on installing and managing Supervisor on Ubuntu and Debian.
  • it did not work. For “tunickname” what to update? the nickname of the droplet or the user?

    • Hi,

      You would need to change the path (/home/your_username/bot/) with the location where you’ve added your bot. For example if you’ve downloaded it to /root/bot then you need to use that as the path.

      Regards,
      Bobby

      • Hello
        another question if you allow me.
        but on “bot” I simply write bot or the filename of my bot?
        But then will the bot always be active on supervisor? i currently use screen

        • Hello,

          Yes, screen is also another alternative. I use it in some cases as well.

          Regarding the path to the file, yes you need to specify the path to your file. If it is not in a bot folder, you don’t have to add it there. For example, if your discord.py file and if that file is stored at the /root directory, then the path will be /root/discord.py.

          If you want to learn more, here is a good introductory tutorial on how to get started with the Linux filesystem:

          https://www.digitalocean.com/community/tutorials/basic-linux-navigation-and-file-management

          Regards,
          Bobby

          by Justin Ellingwood
          If you are new to working with Linux systems, you may be overwhelmed by the prospect of controlling an operating system from the command line. In this guide, we will attempt to get you up to speed with the basics. This guide will not cover everything you need to know to effectively use a Linux system. However, it should give you a good jumping-off point for future exploration.
          • hi

            I tried to start the bot and unfortunately it gave me this error:

            root@*****:~# supervisorctl
            lmiibot                          FATAL     can't find command '/usr/bin/python'
            supervisor>
            root@*****:~#
            

            I don’t think I got anything wrong in the configuration since the file location is right (apparently)

            [program:lmiibot]
            directory=/home/peppe/
            command=/usr/bin/python /home/peppe/LMII-Bot.py
            user=root
            autostart=true
            autorestart=true
            stopasgroup=true
            killasgroup=true
            stderr_logfile=/var/log/discord.err.log
            stdout_logfile=/var/log/discord.out.log
            

            below some pictures of my directory:
            https://cdn.discordapp.com/attachments/841009805266518067/862684202754375710/unknown.png

            I don’t know why it doesn’t go.

          • Hi,

            The error indicates that /usr/bin/python is not available. Are you using python 3? If so you might have to use /usr/bin/python3 instead.

            Regards,
            Bobby

          • hi

            yes i use python3 and i tried. It is running but the bot is not online. Why? I wondered if user=root should be called with another nick.

            EDIT.
            I found myself this sort of mistake: BACKOFF Exited too quickly (process log may have details) why?

          • Hello,

            As per the output that you are getting, to see what is going on, I would recommend that you check out the log file with the supervisor output.

            Also try adding the following line to your supervisor config:

            startsecs=10
            

            Regards,
            Bobby

          • d: 'lmiibot' with pid 419935
            2021-07-17 15:46:39,790 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:46:41,795 INFO spawned: 'lmiibot' with pid 419938
            2021-07-17 15:46:43,245 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:46:46,251 INFO spawned: 'lmiibot' with pid 419940
            2021-07-17 15:46:47,702 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:46:48,705 INFO gave up: lmiibot entered FATAL state, too many start retries too quickly
            2021-07-17 15:49:50,760 INFO spawned: 'lmiibot' with pid 419954
            2021-07-17 15:49:52,236 INFO success: lmiibot entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
            2021-07-17 15:49:52,237 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:49:53,240 INFO spawned: 'lmiibot' with pid 419957
            2021-07-17 15:49:54,306 INFO success: lmiibot entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
            2021-07-17 15:49:54,307 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:49:54,308 INFO spawned: 'lmiibot' with pid 419960
            2021-07-17 15:49:55,740 INFO success: lmiibot entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
            2021-07-17 15:49:55,740 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:49:56,743 INFO spawned: 'lmiibot' with pid 419962
            2021-07-17 15:49:58,155 INFO success: lmiibot entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
            2021-07-17 15:49:58,156 INFO exited: lmiibot (exit status 1; not expected)
            2021-07-17 15:49:58,936 INFO spawned: 'lmiibot' with pid 419964
            

            I made “maintail” and he gave me this.

            i tried to put startsecs=10 but it gave me error.