pythonlinuxubunturebootaiogram

How to implement automatic reloading of the python aiogram bot?


I am creating a telegram bot in python using aiogram. I want to prescribe a condition under which the bot will reboot. Tell me how to implement using the code to restart the bot on aiogram? Thank you in advance for your help!

The bot works on a VPS with Ubuntu 20.04


Solution

  • You will need to create your own service by creating file in /lib/systemd/system folder. For example:

    /lib/systemd/system/myBot.service 
    

    Assuming your bot is in /opt/myBot/ folder - put the following lines into that file:

    [Unit]
    Description=myBot - Telegram bot
    After=network.target
    
    [Service]
    ExecStart=/opt/mybot/myBot.py
    WorkingDirectory=/opt/mybot/
    KillMode=process
    Restart=always
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    

    Save the file and enable service:

    systemctl enable myBbot
    

    Now - start the service

    systemctl start myBot
    

    To check status

    systemctl status myBot
    

    To stop bot

    systemctl stop myBot
    

    If you happen to edit the service file - reload it by issuing

    systemctl daemon-reload