Python Bot永远运行

时间:2018-08-14 10:04:47

标签: python-3.x discord.py

如果bot随时崩溃,如何使我的bot永远运行。

我在Linux服务器上使用screenputty中运行此 main.py 文件,但是我需要运行另一个文件来检查此main.py文件是否正在运行如果不是30分钟,则应重新启动main.py文件。

所以下面是我的 main.py

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print (bot.user.name)
    print (bot.user.id)
    print ("_____")

@bot.command(pass_context=True)
async def first(ctx):
     await bot.say("Hello {}".format(ctx.message.author.mention))

bot.run("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

1 个答案:

答案 0 :(得分:2)

注意:我的回答以更通用的方式解决了这个问题。如果您的问题涉及不和谐的漫游器,this answer将提供更好的解决方案。


原始答案:

您可以将对程序的调用包装到bash脚本中。这里是一个例子:

#!/bin/bash

while true
do
  sleep 0.5
  ./bot # your program
  echo "[$(date)] bot exited with code $?. restarting ..."
done

以上脚本将在程序退出时重新启动程序。但是如果我是你,我会附上一份检查报告,如果它经常崩溃,它将停止重新启动您的脚本。

#!/bin/bash

FAILS=0

while true
do
  sleep 0.5
  ./bot # your program
  EXIT=$?
  ((FAILS++))

  if [[ $FAILS -gt 10 ]]
  then
    echo "[$(date)] failed to many times. aborting ..."
    exit 1
  fi

  echo "[$(date)] bot exited with code $EXIT. restarting ..."

done
相关问题