【问题标题】:How to implement automatic reloading of the python aiogram bot?如何实现python aiogram bot的自动重新加载?
【发布时间】:2021-08-16 14:57:16
【问题描述】:

我正在使用 aiogram 在 python 中创建一个电报机器人。我想规定一个机器人将重新启动的条件。告诉我如何使用代码重新启动 aiogram 上的机器人?提前感谢您的帮助!

该机器人在带有 Ubuntu 20.04 的 VPS 上运行

【问题讨论】:

    标签: python linux ubuntu reboot aiogram


    【解决方案1】:

    您需要通过在 /lib/systemd/system 文件夹中创建文件来创建自己的服务。 例如:

    /lib/systemd/system/myBot.service 
    

    假设您的机器人位于 /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
    

    保存文件并启用服务:

    systemctl enable myBbot
    

    现在 - 启动服务

    systemctl start myBot
    

    查看状态

    systemctl status myBot
    

    停止机器人

    systemctl stop myBot
    

    如果您碰巧编辑了服务文件 - 通过发出重新加载它

    systemctl daemon-reload
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2017-05-10
      • 1970-01-01
      • 2023-01-04
      • 2016-09-03
      • 1970-01-01
      相关资源
      最近更新 更多