Summary
My Telegram bot persists even after terminating its task and removing its file. I have replaced my previous code with the new example below. How can I completely stop the bot?
from flask import Flask
from aiogram import Bot, Dispatcher, executor, types
proxy_server = "http://alternate.proxy:9000"
app_instance = Flask(__name__)
telegram_bot = Bot(token='your_new_token', proxy=proxy_server)
bot_dispatcher = Dispatcher(telegram_bot)
async def startup_sequence():
print("Bot initialized successfully")
@bot_dispatcher.message_handler(commands=['assist'])
async def provide_help(message: types.Message):
await telegram_bot.send_message(message.chat.id, "Assistance is here!")
@bot_dispatcher.message_handler()
async def replicate_message(message: types.Message):
await message.reply(message.text)
executor.start_polling(bot_dispatcher, skip_updates=True, on_startup=startup_sequence())
i had a simmilar issue. it seems like the bot process didn’t really exit after you stopped the program. try check your running python processes and kill them manualliy, sometimes using nohup or supervisor can keep them alive
It appears that the bot may indeed be lingering as an orphan process even after you terminate the script. In my experiments, I discovered that using process management tools allowed me to see all Python processes still running in the background. I found that sometimes using nohup or a similar command can inadvertently leave processes that continue running. A possible remedy is to cross-check running processes and terminate those associated with your bot. This approach helped me ensure that my device didn’t have multiple instances consuming resources, leading to more predictable shutdown behavior.
I encountered a similar situation when integrating aiogram with my own Flask app. My experience showed that the asynchronous event loop from the polling process may not always terminate as expected. I ended up adding explicit cleanup logic to cancel pending tasks and properly close the event loop when shutting down my application. This involved listening for termination signals and then calling shutdown procedures to avoid leaving orphaned processes in the background. Reviewing Python’s asynchronous cancellation and shutdown routines helped me ensure that my bot and its tasks ceased completely.
try explicitly stopping your async loop before exit, sometimes it stickis and keeps processes runnng. i ended mine by cancelling pending tasks and forcing loop.stop(); also check for stray threads that might remain after shutdown.