# 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).

Source: https://livops.dev/en/guides/keep-telegram-bot-running-24-7

By Livops team, the people who build and run Livops. Updated 3 December 2026.

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](https://livops.dev/en/guides/error-telegram-conflict-getupdates).

### 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](https://livops.dev/en/guides/telegram-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:

```js
// 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:

```ini
# /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:

```sh
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].

```sh
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.** `getWebhookInfo` returns `pending_update_count`, the number of updates awaiting delivery, and for a webhook `last_error_date` and `last_error_message` for 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](https://livops.dev/en/guides/telegram-getwebhookinfo).
- **Watch from outside.** A small scheduled job on another machine that calls `getWebhookInfo` and 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_id` before 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

1. 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.
2. 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.
3. 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:

```json
{
  "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](https://livops.dev/en/guides/run-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 `getWebhookInfo` about 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 on `PORT`, checks `getWebhookInfo` about every ten minutes, and puts the webhook back if something moved it. Your code does not call `setWebhook`.
- **Logs.** The project's **Logs** page shows what the bot prints; over [MCP](https://livops.dev/en/docs/mcp), `get_logs` returns up to 5000 lines.
- **Data.** Files written to `/app/data` (the `DATA_DIR` variable) are kept across restarts and deploys; `/tmp` is 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](https://livops.dev/en/docs/pricing) for plans, and [Telegram bot hosting](https://livops.dev/en/hosting/telegram-bot) 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

1. [Telegram Bot API — Getting updates, getUpdates, setWebhook, getWebhookInfo](https://core.telegram.org/bots/api). Checked 25 November 2026.
2. [Linux manual pages — signal(7)](https://man7.org/linux/man-pages/man7/signal.7.html). Checked 25 November 2026.
3. [GNU Coreutils — nohup invocation](https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html). Checked 25 November 2026.
4. [Linux manual pages — tmux(1)](https://man7.org/linux/man-pages/man1/tmux.1.html). Checked 25 November 2026.
5. [systemd — systemd.service](https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html). Checked 25 November 2026.
6. [systemd — systemd.unit](https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html). Checked 25 November 2026.
7. [systemd — systemd-system.conf](https://www.freedesktop.org/software/systemd/man/latest/systemd-system.conf.html). Checked 25 November 2026.
8. [Docker Docs — Start containers automatically](https://docs.docker.com/engine/containers/start-containers-automatically/). Checked 25 November 2026.
9. [Docker Docs — docker container stop](https://docs.docker.com/reference/cli/docker/container/stop/). Checked 25 November 2026.
10. [aiogram — Long-polling (3.34.0)](https://docs.aiogram.dev/en/latest/dispatcher/long_polling.html). Checked 25 November 2026.
11. [aiogram source — aiogram/dispatcher/dispatcher.py](https://raw.githubusercontent.com/aiogram/aiogram/dev-3.x/aiogram/dispatcher/dispatcher.py). Checked 25 November 2026.
12. [python-telegram-bot — Application (v22.8)](https://docs.python-telegram-bot.org/en/stable/telegram.ext.application.html). Checked 25 November 2026.
13. [python-telegram-bot source — telegram/ext/_application.py](https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/src/telegram/ext/_application.py). Checked 25 November 2026.
14. [grammY — Scaling up IV, reliability](https://grammy.dev/advanced/reliability). Checked 25 November 2026.
15. [Telegraf — documentation (v4.16.3)](https://telegraf.js.org/). Checked 25 November 2026.
16. [Python documentation — sys](https://docs.python.org/3/library/sys.html). Checked 25 November 2026.
17. [Render — Background workers](https://render.com/docs/background-workers). Checked 3 December 2026.
18. [Render — Pricing](https://render.com/pricing). Checked 3 December 2026.
19. [Render — Deploy for free](https://render.com/docs/free). Checked 3 December 2026.
20. [Railway — Pricing](https://railway.com/pricing). Checked 3 December 2026.
21. [Railway — Restart policy](https://docs.railway.com/deployments/restart-policy). Checked 3 December 2026.
22. [Fly.io — Pricing](https://docs.fly.io/about/pricing). Checked 3 December 2026.
23. [Fly.io — Machine restart policy](https://docs.fly.io/machines/guides-examples/machine-restart-policy). Checked 3 December 2026.
24. [PythonAnywhere — Pricing](https://www.pythonanywhere.com/pricing/). Checked 3 December 2026.
25. [PythonAnywhere — Always-on tasks](https://help.pythonanywhere.com/pages/AlwaysOnTasks/). Checked 3 December 2026.
26. [SmartCloud — VDS plans](https://smartcloud.uz/cloud-services/vds/). Checked 3 December 2026.
