简体   繁体   English

如何实现python aiogram bot的自动重新加载?

[英]How to implement automatic reloading of the python aiogram bot?

I am creating a telegram bot in python using aiogram.我正在使用 aiogram 在 python 中创建一个电报机器人。 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?告诉我如何使用代码在 aiogram 上重新启动机器人? Thank you in advance for your help!预先感谢您的帮助!

The bot works on a VPS with Ubuntu 20.04该机器人在装有 Ubuntu 20.04 的 VPS 上运行

You will need to create your own service by creating file in /lib/systemd/system folder.您需要通过在 /lib/systemd/system 文件夹中创建文件来创建自己的服务。 For example:例如:

/lib/systemd/system/myBot.service 

Assuming your bot is in /opt/myBot/ folder - put the following lines into that file:假设您的机器人在 /opt/myBot/ 文件夹中 - 将以下行放入该文件:

[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM