如何保持永远在线的任务运行?

问题描述 投票:0回答:1

嘿,所以我正在努力让我一直在执行的任务保持运行。它说它刚刚开始,并且在任务日志中显示它要求提供电话号码。我从文件页面启动脚本。该脚本显示其正在运行,并且运行正常。

我在这里 https://help.pythonanywhere.com/pages/AlwaysOnTasks/ 寻求帮助并尝试了它所说的操作。我想通过从文件页面运行脚本,然后单击此处的运行。然后让我一直在执行的任务真正发挥作用。

当我从 bash 控制台启动脚本时,我确实让任务始终运行。这需要与 python 3.10 不同的启动任务。从

运行任务

https://help.pythonanywhere.com/pages/AlwaysOnTasks/

我在任务中尝试了这两个命令。 python3.10/home python3.10 -u /home

在日志中是这么说的

“Jan 15 23:26:47 请输入您的电话(或机器人令牌):意外错误:读取行时出现 EOF Jan 15 23:26:47 bash:第 1 行:6 分段错误(核心已转储) bash -l -c 'python3.10"

我希望能够添加一个始终开启的任务,以便我可以从文件夹运行脚本。

这是脚本

from telethon import TelegramClient, events
from datetime import datetime
import pytz

# Your API credentials
api_id = 'api id'
api_hash = 'hash '

# The username or user ID of the user whose messages you want to monitor
specific_user = '@'

# The group where messages will be monitored
source_group = '@'

# The group where messages will be forwarded
destination_group = '@'

# Keywords to filter messages
keywords = ['root', '', 'keyword3']

# Create a Telegram client
client = TelegramClient('session_name', api_id, api_hash)

# Function to get PST time
def get_pst_time():
    pst = pytz.timezone('America/Los_Angeles')
    return datetime.now(pst).strftime('%Y-%m-%d %H:%M:%S')

@client.on(events.NewMessage(chats=source_group))
async def forward_specific_user_messages(event):
    # Check if the message is from the specific user
    if event.sender_id == specific_user or event.sender.username == specific_user:
        # Check if the message contains any of the keywords
        if any(keyword.lower() in event.message.message.lower() for keyword in keywords):
            # Forward the message to the destination group
            await client.send_message(destination_group, event.message)

            # Log the action with a 5-word preview
            message_preview = ' '.join(event.message.message.split()[:5]) + ('...' if len(event.message.message.split()) > 5 else '')
            print(f"[{get_pst_time()}] Forwarded message: '{message_preview}'")

# Start the client
with client:
    print("Listening for messages...")
    client.run_until_disconnected()
scheduled-tasks pythonanywhere
1个回答
0
投票
PythonAnywhere 上的始终开启任务是为非交互式脚本设计的,因此如果您的脚本提示输入,它将引发错误并退出。这将使它进入重新启动状态,然后它会再次运行,再次崩溃,等等。 这就是为什么你会看到它处于“准备启动”状态 - 它大部分时间都在等待重新启动,然后运行几毫秒并再次崩溃。

要让您的脚本作为始终开启的任务运行,您需要对其进行修改,以便它不会提示输入电话号码/机器人令牌;如果不是您自己的代码执行此操作,而是您的代码所依赖的某种库,那么您应该检查该库的文档以了解如何执行此操作。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.