How to keep a Telegram bot running 24/7
A bot run with python bot.py or node bot.js stops when the terminal closes, the laptop sleeps or the code crashes, and two copies polling one token knock each other off with 409 Conflict. Keep exactly one copy running under something that restarts it — systemd, Docker or a platform — and stop it cleanly on SIGTERM. On Livops a bot runs from a push for 19 credits a month (19,000 so'm).
By Livops team, the people who build and run LivopsUpdated
A Telegram bot works only while its process is running, so “my bot stopped answering” almost always means the process stopped — or that a second copy is fighting the first for the same token. This page goes through why bots die, what polling and webhooks change about uptime, how to run aiogram, python-telegram-bot, grammY and Telegraf bots under something that restarts them, how to keep a crash loop from turning into an outage nobody notices, and what Livops does for a bot on its own.
Why do Telegram bots stop?
The terminal closes, or the laptop sleeps
A bot started with python bot.py or node bot.js belongs to its terminal. When the window closes or the SSH connection drops, the process gets SIGHUP, and the default action of SIGHUP is to terminate[2]. A laptop that goes to sleep pauses the bot with it. Fine for trying the bot; no way to run it.
It crashes, and nothing starts it again
An unexpected message, a network error the library does not retry, a database that restarts: an unhandled exception ends the process. Without a process manager it stays down until somebody looks.
Two copies poll with one token
Telegram hands updates to one getUpdates caller at a time. Start the bot on a server while the copy on your laptop still runs — or run it in two web workers, or let a deploy start the new version before the old one stops — and Telegram ends one copy’s request with 409 Conflict: terminated by other getUpdates request. Neither copy crashes; they take turns cutting each other off, and messages go to whichever holds the connection. Causes and fixes: Conflict: terminated by other getUpdates request.
The webhook is wrong, or gone
A webhook bot is only reachable while its HTTPS address answers. A certificate that expired, a port Telegram does not use, a server that moved: Telegram retries a failed delivery and gives up after a reasonable number of attempts[1]. And while a webhook is set, getUpdates does not work at all[1], so a bot moved back to polling with the old webhook left behind receives nothing.
Polling or webhook: which keeps a bot up?
Neither is more reliable by itself; they fail in different places.
- Polling needs a process that never stops and outbound internet. Nothing to set up, but anything that puts idle processes to sleep stops the bot.
- A webhook needs a public HTTPS address that answers every time. The process can be a web server, but if it is down, Telegram’s deliveries fail.
Either way, incoming updates are kept on Telegram’s server until the bot receives them, for no longer than 24 hours[1]: a bot that is back within minutes has lost nothing. Either way, something has to restart it. The full comparison, with how each library switches between the two: Webhook vs polling.
The options at a glance
| Option | What it is | Smallest paid always-on process | What you still maintain |
|---|---|---|---|
nohup / tmux on a VPS |
The bot survives your logout, nothing more | A VPS in Uzbekistan: 82,000 so’m a month, 1 vCPU, 2 GB, 30 GB HDD (SmartCloud)[26] | Everything, and restarting it by hand |
| systemd on a VPS | A service started at boot and restarted on exit | The same VPS | The system, updates, deploys, HTTPS for a webhook, alerts |
| Docker on a VPS | A container with a restart policy | The same VPS | Docker, the image, the system, alerts |
| Render | A background worker | $7 a month: 0.5 CPU, 512 MB[18] | Your code |
| Railway | A service billed by usage | Hobby plan: $5 a month, including the same amount of usage[20] | Your code, the restart policy |
| Fly.io | A Machine billed by the second | $2.19 a month: shared-cpu-1x, 256 MB[22] | Your code, the restart policy |
| PythonAnywhere | An always-on task (Python only) | Developer plan: $10 a month, one always-on task[24] | Your code |
| Livops | A bot service; servers in Uzbekistan | 19 credits (19,000 so’m) a month | Your code |
Other providers’ prices are from each provider’s own page, read on 25 November 2026; the dates are in Sources below. Livops prices are the ones the cabinet bills today.
First: make the bot stop cleanly
Every option below stops a bot the same way: SIGTERM, then SIGKILL a few seconds later — 10 seconds for docker stop[9], 90 by default for systemd[7]. A polling bot killed mid-batch has not told Telegram which updates it handled, and grammY’s guide warns that it may process up to 100 of them again[14]. Each library’s start command decides whether SIGTERM is handled:
| Library | Start polling | On SIGTERM |
|---|---|---|
| aiogram 3 | asyncio.run(main()) with await dp.start_polling(bot)[10] |
Handled: handle_signals is True by default[11]; it waits up to tasks_shutdown_timeout for running handlers[10] |
| python-telegram-bot 22 | app.run_polling() |
Handled: stops on SIGINT, SIGTERM and SIGABRT by default outside Windows[13], with a graceful shutdown[12] |
| grammY 1 | bot.start() |
Yours to add: process.once("SIGTERM", () => bot.stop())[14] |
| Telegraf 4 | bot.launch() |
Yours to add: process.once('SIGTERM', () => bot.stop('SIGTERM'))[15] |
aiogram cannot install its signal handlers on Windows[10], which matters only if you run the bot there. For the two Node libraries the lines are short, and the official examples include them:
// grammY
bot.start();
process.once("SIGINT", () => bot.stop());
process.once("SIGTERM", () => bot.stop());
// Telegraf
bot.launch();
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));
Two more things belong in the bot before it goes anywhere: the token read from the environment (os.environ["BOT_TOKEN"], process.env.BOT_TOKEN), and, in Python, unbuffered output. When standard output is not a terminal it is block-buffered[16], so print() lines reach the log late or never; run python -u or set PYTHONUNBUFFERED=1[16], or use logging.
Each option
nohup and tmux
nohup runs the bot with hangup signals ignored; you add & yourself, and output goes to nohup.out[3]. A tmux session can be detached, keeps running, and survives an SSH timeout[4]. Both fix the terminal problem only: a crash leaves the bot dead, and a reboot ends it with every other process. Good for an evening of testing on a server, not for a bot people rely on.
A systemd service
The standard way on a VPS. One unit, for a Python bot:
# /etc/systemd/system/mybot.service
[Unit]
Description=Telegram bot
After=network-online.target
Wants=network-online.target
[Service]
Type=exec
User=bot
WorkingDirectory=/opt/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/opt/mybot/.venv/bin/python -u bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
For grammY or Telegraf, ExecStart=/usr/bin/node bot.js (check the path with which node). /etc/mybot.env holds BOT_TOKEN=…, readable only by root. Then:
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
journalctl -u mybot -f
Restart=always restarts the bot whether it exited cleanly or not[5], and enable makes it start at boot through WantedBy=[6]. Keep RestartSec=5: the default delay is 100 ms[5], and by default a unit started more than 5 times in 10 seconds is not started again[7][6], so a bot that crashes on start — a bad token, a missing variable — would give up within a second and stay down. With five seconds between tries it never reaches the limit and keeps retrying until you fix it.
To update the bot: git pull, then sudo systemctl restart mybot. What systemd does not do is tell you anything: a bot in a restart loop looks just as quiet as a bot that works.
Docker with a restart policy
With a Dockerfile whose CMD is the start command above, --restart always (or restart: always in Compose) restarts the container whenever it stops, and brings back even a container you stopped by hand when the Docker daemon restarts; unless-stopped does not bring back a stopped one; on-failure restarts only on a non-zero exit[8].
docker run -d --name mybot --restart always --env-file mybot.env mybot
A restart policy only applies after a container has stayed up for 10 seconds[8]. docker stop gives the bot 10 seconds after SIGTERM on Linux[9] — enough for all four libraries’ shutdown. To deploy, stop the old container before you start the new one: two containers running side by side are two pollers.
Render
A background worker runs continuously and receives no incoming traffic[17], which is exactly a polling bot. The smallest paid instance is $7 a month for 0.5 CPU and 512 MB[18]. Background workers cannot use free instances[19]. A webhook bot can run as a web service instead, but a free web service spins down after 15 minutes without inbound traffic and takes about a minute to start again[19] — long enough for Telegram’s deliveries to fail in the meantime.
Railway
Services on Hobby and above run always-on; Hobby is $5 a month including that much usage, paid by credit card[20]. The default restart policy is On Failure with at most 10 restarts[21]: a bot that crashes an eleventh time stays down. Always is available on paid plans, not on Free or trial[21].
Fly.io
The smallest Machine is $2.19 for 30 days of continuous running, billed by the second[22]. Machines created by fly launch and fly deploy get on-fail, which retries non-zero exits up to 10 times in 5 minutes and then lets the Machine stop; Fly.io recommends always for an always-on app with no services[23] — a polling bot. A credit card is required for every organization apart from linked ones[22].
PythonAnywhere
For an aiogram or python-telegram-bot bot, an always-on task is restarted automatically if it crashes, apart from during system maintenance, and works only on paid accounts[25]; the Developer plan is $10 a month with one task[24]. The free Beginner account has no always-on tasks and reaches only specific sites over HTTP(S)[24].
Livops
A bot is its own service type: always running, polling by default, with the webhook set up for you if you switch to one. A push is the deploy, the restarts are the platform’s, and you are told when the bot keeps crashing or when another copy polls with its token. Payment is in so’m through Payme or Click, with no card, and the servers are in Uzbekistan, close to users there. You do not get root; a bot that needs system software of its own is better on a VPS or in its own Dockerfile.
Restarts, crash loops and knowing it is down
Restarting is half the job. The other half is noticing that the restarts are not helping.
- A crash loop is a bot that is down. A bot with a revoked token or a missing variable crashes on start every time. systemd gives up after 5 starts in 10 seconds unless you space them out[7]; Railway stops after 10 restarts[21]; Fly.io after 10 failures in 5 minutes under
on-fail[23]. Spacing restarts out keeps the bot trying, but only fixing the cause brings it back, and you have to know. - Ask Telegram.
getWebhookInforeturnspending_update_count, the number of updates awaiting delivery, and for a webhooklast_error_dateandlast_error_messagefor the most recent failed delivery[1]. A count that keeps growing while your bot is “running” means nothing is reading. How to read it: getWebhookInfo. - Watch from outside. A small scheduled job on another machine that calls
getWebhookInfoand messages you when the count grows catches every failure mode above, including one where the process is up but stuck. - Make repeats harmless. Delivery is at least once: a hard kill can replay recent updates[14], and a webhook delivery is retried[1]. Check
update_idbefore charging a card twice.
VPS or platform: what do you maintain?
On a VPS the bot runs exactly as you set it up: systemd or Docker restarts it, and everything around that — the operating system, its updates, deploying without two pollers, HTTPS for a webhook and the alert that tells you it is down — is yours. You get root and room for many bots on one machine. On a platform you hand over the code and the platform keeps it running; you give up root. One bot and no wish to run a server: a platform. A dozen bots and a liking for Linux: a VPS.
Where can you keep a bot running for free?
- Livops has no free tier for bots. On an account that has not paid yet, the first bot or app runs for 15 minutes on a zero balance, once, so you can watch it answer; static sites are free.
- Render gives free instances to web services, Postgres and Key Value, not background workers, and a free web service spins down after 15 minutes without inbound traffic[19].
- Railway’s Free plan includes $5 of usage a month, up to 1 vCPU and 0.5 GB per service; services sleep after 5 to 10 minutes without outbound traffic; a trial gives $5 of credit for 30 days with no card[20]. On Free and trial plans the Always restart policy is not available[21].
- Fly.io starts every new account on a Free plan: $1 of credit with no card, and $5 of usage a month once a card is added, with limits such as up to 3 Machines[22].
- PythonAnywhere’s free account has no always-on tasks[24].
Keeping a bot running on Livops
- In the cabinet, create a project from the GitHub repository, upload the folder or a
.zip, or start from the aiogram 3 bot + Redis template. A bot built on aiogram, python-telegram-bot, pyTelegramBotAPI, Telegraf or grammY is recognised as a Telegram bot by its library. - Paste the token BotFather gave you into the Bot token field, or add it under Variables as
BOT_TOKEN, marked as a secret: its value is never shown again. - Deploy. The log shows the build, and the bot answers in Telegram once it starts.
The start command is detected — bot.py, main.py, app.py or run.py for Python, the start script in package.json for Node. To name it yourself, put livops.json in the repository root:
{
"kind": "bot",
"start": ".venv/bin/python bot.py"
}
For grammY or Telegraf, "start": "node bot.js". kind: bot is the Telegram bot type; a Discord bot or any other always-on process is a worker — see Run a Python script 24/7.
What keeps it running:
- Never two copies. On every push the old bot is stopped first —
SIGTERM, then ten seconds before it is killed, the window the shutdown above has to fit in — and the new version starts after it. Messages sent in between wait at Telegram. If the build fails, the running version is left alone. - The first 30 seconds are watched. If the bot exits, or its log shows another copy polling with the same token or a webhook still set, the deploy fails with the reason in its log instead of going green.
- Restarts, with a limit and a message. If the bot crashes, it is started again, and the project page counts the restarts. Ten crashes inside 15 minutes and it is stopped, and you are told with the last line it printed — in the cabinet, and in Telegram if your account is linked to the Livops bot.
- Silent failures are reported too. If, while the bot runs, its log keeps showing a 409 conflict, the project page stops showing it as working and you get a notification. About every ten minutes the platform asks Telegram’s
getWebhookInfoabout a polling bot, and warns you if a webhook is set or if messages pile up while the process runs. - A webhook you never set. Switch Settings → Connection to Telegram to Webhook and press Save and redeploy: the platform generates
WEBHOOK_SECRET, tells Telegram the address once the new version answers onPORT, checksgetWebhookInfoabout every ten minutes, and puts the webhook back if something moved it. Your code does not callsetWebhook. - Logs. The project’s Logs page shows what the bot prints; over MCP,
get_logsreturns up to 5000 lines. - Data. Files written to
/app/data(theDATA_DIRvariable) are kept across restarts and deploys;/tmpis wiped.
A bot is 19 credits a month (19,000 so’m), polling or webhook. One credit is 1,000 so’m, and bought credits never expire. See Pricing for plans, and Telegram bot hosting for how the options compare.
Questions
Why does my bot stop when I close the terminal?
Closing the terminal or losing the SSH connection sends the process a hangup signal, which ends it by default. A laptop that sleeps pauses it too. Run the bot on a machine that stays on, under systemd, Docker or a platform that restarts it.
Why does my bot answer only some messages, with 409 Conflict in the log?
Two copies are polling with the same token, often one still running on your computer and one on the server. Telegram cuts one off each time the other asks for updates. Stop the extra copy, or generate a new token in BotFather and use it only where the bot should run.
Is a webhook more reliable than polling?
Not by itself. A polling bot needs a process that never stops; a webhook bot needs an HTTPS address that always answers. Either way Telegram keeps undelivered updates for up to 24 hours, and either way something has to restart the bot when it crashes.
Will my bot lose messages while it restarts?
Not if it is back within 24 hours: Telegram stores incoming updates until the bot receives them, for at most a day. A bot that is killed without stopping cleanly may handle a few recent updates twice, so make payments and orders safe to repeat.
How do I know my bot is down?
Call getWebhookInfo: a pending_update_count that keeps growing while the bot is supposedly running means nothing is reading the updates, and for a webhook last_error_message says why delivery fails. On Livops you are told when the bot keeps crashing, when another copy polls with its token, and when updates pile up, in Telegram if your account is linked.
How much does it cost to keep a bot running on Livops?
19 credits a month, 19,000 so'm, polling or webhook. Credits are bought in so'm through Payme or Click, with no card. There is no free tier for bots; the first bot on an account that has not paid yet runs for 15 minutes on a zero balance, once.
Sources
- Telegram Bot API — Getting updates, getUpdates, setWebhook, getWebhookInfo (core.telegram.org). Checked .
- Linux manual pages — signal(7) (man7.org). Checked .
- GNU Coreutils — nohup invocation (gnu.org). Checked .
- Linux manual pages — tmux(1) (man7.org). Checked .
- systemd — systemd.service (freedesktop.org). Checked .
- systemd — systemd.unit (freedesktop.org). Checked .
- systemd — systemd-system.conf (freedesktop.org). Checked .
- Docker Docs — Start containers automatically (docs.docker.com). Checked .
- Docker Docs — docker container stop (docs.docker.com). Checked .
- aiogram — Long-polling (3.34.0) (docs.aiogram.dev). Checked .
- aiogram source — aiogram/dispatcher/dispatcher.py (raw.githubusercontent.com). Checked .
- python-telegram-bot — Application (v22.8) (docs.python-telegram-bot.org). Checked .
- python-telegram-bot source — telegram/ext/_application.py (raw.githubusercontent.com). Checked .
- grammY — Scaling up IV, reliability (grammy.dev). Checked .
- Telegraf — documentation (v4.16.3) (telegraf.js.org). Checked .
- Python documentation — sys (docs.python.org). Checked .
- Render — Background workers (render.com). Checked .
- Render — Pricing (render.com). Checked .
- Render — Deploy for free (render.com). Checked .
- Railway — Pricing (railway.com). Checked .
- Railway — Restart policy (docs.railway.com). Checked .
- Fly.io — Pricing (docs.fly.io). Checked .
- Fly.io — Machine restart policy (docs.fly.io). Checked .
- PythonAnywhere — Pricing (pythonanywhere.com). Checked .
- PythonAnywhere — Always-on tasks (help.pythonanywhere.com). Checked .
- SmartCloud — VDS plans (smartcloud.uz). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your botRelated
- sqlite3.OperationalError: unable to open database file (and attempt to write a readonly database): how to fix itsqlite3.OperationalError: unable to open database file means SQLite could not open or create the file at the path it was given: the folder does not exist, a relative path was resolved from another working directory, or the folder is not writable. Attempt to write a readonly database means the file opened, but only for reading. SQLite also writes -journal and -wal files next to the database, so the folder must be writable, not just the file.
- Telegram bot hosting in 2026: the options comparedA Telegram bot needs a process that never stops, or a public HTTPS address for a webhook. On Livops it runs from a GitHub push for 19 credits a month (19,000 so'm). Here is how a VPS, Railway, Render, Fly.io and Vercel compare.
- Telegram bot webhook vs polling: which one to usePolling means your bot asks Telegram for updates in a loop; a webhook means Telegram POSTs each update to your HTTPS address. Polling is simpler and needs no address; a webhook needs HTTPS but no always-running loop. A bot uses one or the other, never both. On Livops both run for the same 19 credits a month.
- Conflict: terminated by other getUpdates request — how to fix itTelegram is telling your bot that another process called getUpdates with the same token and took the connection over. Two copies of the bot are polling at once: stop the other one (usually the copy still running on your computer or an old server), or get a new token from @BotFather, and the error stops.
- getWebhookInfo: check why your Telegram webhook is not workingCall https://api.telegram.org/bot<TOKEN>/getWebhookInfo. An empty url means no webhook is set. A pending_update_count that keeps growing with a recent last_error_date means Telegram cannot deliver, and last_error_message says why: a non-2XX answer, a timeout, a TLS error. Fix the endpoint, then reset with setWebhook, or deleteWebhook with drop_pending_updates to discard the backlog.
- How to run a Python script 24/7 (and keep it running)A script started in a terminal stops when the terminal closes, the SSH connection drops or the laptop sleeps. To keep it running you need something that starts it again: a systemd service with Restart=always, Docker with a restart policy, or a platform's background worker. On Livops it runs as a worker for 19 credits a month (19,000 so'm).
- aiogram bot hosting: how to deploy an aiogram 3 botAn aiogram 3 bot needs one process that never stops, the token in an environment variable, state kept outside memory and a clean exit on SIGTERM. On Livops you push a repository with bot.py and requirements.txt, and it runs for 19 credits a month (19,000 so'm), polling or webhook.
- python-telegram-bot hosting: deploying a v22 botA python-telegram-bot bot runs as one Application: run_polling() for polling, run_webhook() with the webhooks extra for a webhook. Both stop cleanly on SIGTERM. On Livops it runs from a push with bot.py and requirements.txt for 19 credits a month (19,000 so'm).
- grammY bot deploy: polling, webhooks and hostingDeploy a grammY bot as one always-on Node.js process: bot.start() for polling, or webhookCallback() behind an HTTP server, with bot.stop() on SIGTERM. On Livops a repository with grammy in package.json runs as a bot from every push, for 19 credits a month (19,000 so'm).
- Telegraf bot hosting: deploying a Telegraf 4 bot on Node.jsA Telegraf bot is one Node.js process: bot.launch() for polling, or bot.webhookCallback() behind an HTTP server for a webhook, with bot.stop() on SIGTERM. On Livops a repository with telegraf in package.json is run as a bot from every push, for 19 credits a month (19,000 so'm).
- Free Telegram bot hosting in 2026: what actually worksA polling bot needs a process that never stops, and few free tiers keep one: Railway's Free plan does within $5 of usage a month, Fly.io's Free plan covers the smallest Machine once a card is added, and Oracle's Always Free VMs may be reclaimed when idle. Render's free web service, Cloud Run and PythonAnywhere's free web app suit only a webhook, and sleep between messages. Livops is not free for bots: one 15-minute demonstration, then 19 credits a month.