---
title: "Run Discord and Telegram bots 24/7 on a small VPS · CloudSat"
description: "Run Discord and Telegram bots 24/7 on a VPS: what each platform needs from the server, a systemd or Docker setup, webhooks, secrets, data and monitoring."
url: https://cloudsat.io/learn/run-discord-and-telegram-bots-on-a-vps
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[DevOps](https://cloudsat.io/learn/topic/devops-and-automation)

# Run Discord and Telegram bots 24/7 on a small VPS

Run Discord and Telegram bots 24/7 on a VPS: what each platform needs from the server, a systemd or Docker setup, webhooks, secrets, data and monitoring.

CloudSat team November 18, 2026 7 min read

A bot on a laptop dies when the lid closes, and a bot on a free tier gets put to sleep or recycled. A small VPS keeps it connected around the clock, but only if it restarts itself after a crash, keeps its token out of reach and holds on to its data through every redeploy. This guide covers what Discord and Telegram actually need from a server, then a setup that runs one bot or ten.

## What each platform needs from your server

The two platforms deliver events in different ways, and that decides your network setup:

|  | Discord | Telegram |
| --- | --- | --- |
| How events arrive | The bot opens a WebSocket to the Gateway and keeps it alive with heartbeats | Long polling with `getUpdates`, or a webhook that Telegram calls |
| Inbound port | None, unless you add an HTTP interactions endpoint | None for polling; 443, 80, 88 or 8443 for a webhook |
| TLS | Only for an interactions endpoint | Webhooks need TLS 1.2 or later |
| IP version | Outbound IPv4: the Gateway hostname had no IPv6 address when this was written | Webhooks are IPv4 only |
| Limit to know | Sharding is required from 2,500 servers | Up to 100 parallel webhook connections, 40 by default |

Two Discord details catch people out. Reading message text requires the Message Content intent, which is privileged: switch it on in the Developer Portal, and expect an approval step once the app is verified, which is required from 100 servers. And each shard handles at most 2,500 servers, so a growing bot needs sharding support in its library before it hits that number.

### Polling or webhook for Telegram?

Polling is the simple choice: the bot asks Telegram for new updates in a loop, needs no domain or certificate, and works on any server with outbound internet. A webhook makes Telegram push each update to an HTTPS URL you own, which is more efficient for busy bots but needs a public IPv4 address, a DNS name and TLS. The two cannot run together: `getUpdates` stops working while a webhook is set. For one small bot, start with polling.

## Size the server

Most bots spend their lives waiting on the network, so CPU is rarely the limit. Memory is: libraries that cache servers, members and messages grow with the communities the bot joins. Start small, then read the real number from `systemctl status`, which prints each service's memory use, and resize when a bot's peak approaches the plan's limit. On CloudSat, the smallest [Cloud VPS](https://cloudsat.io/cloud-vps) starts at $1.98 a month and bills hourly, so a new bot can run for a day of testing before you commit to a month.

## Run it under systemd

systemd restarts a crashed bot, starts it at boot and collects its logs. Create a system user, put the code where that user can read but not change it, and keep the token in a root-only file:

```bash
sudo apt install -y python3-venv git
sudo useradd --system --home-dir /opt/bots --shell /usr/sbin/nologin bots
sudo git clone https://git.example.com/you/welcome-bot.git /opt/bots/welcome
sudo python3 -m venv /opt/bots/welcome/.venv
sudo /opt/bots/welcome/.venv/bin/pip install -r /opt/bots/welcome/requirements.txt
sudo mkdir -p /etc/bots
sudo install -m 600 /dev/null /etc/bots/welcome.env
sudoedit /etc/bots/welcome.env
```

The env file holds lines such as `DISCORD_TOKEN=...`. systemd reads it as root before dropping privileges, so the bot receives the variable without being able to read the file. Then write one template unit, `/etc/systemd/system/bot@.service`, that serves every bot:

```ini
[Unit]
Description=Chat bot %i
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=600
StartLimitBurst=10

[Service]
User=bots
WorkingDirectory=/opt/bots/%i
EnvironmentFile=/etc/bots/%i.env
ExecStart=/opt/bots/%i/.venv/bin/python bot.py
Restart=on-failure
RestartSec=5
StateDirectory=bots/%i
MemoryMax=400M
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl daemon-reload
sudo systemctl enable --now bot@welcome
journalctl -u bot@welcome -f
```

`%i` is the instance name, so `bot@welcome` and `bot@support` each get their own code folder, env file and data directory. `StateDirectory=` creates `/var/lib/bots/welcome`, owned by the service user, and passes its path to the bot as `$STATE_DIRECTORY`. `ProtectSystem=strict` makes the rest of the filesystem read-only for the bot, `MemoryMax=` stops one leaking bot from starving the others, and the start limit keeps a crash loop from spinning forever: after ten failed starts in ten minutes, systemd gives up and `systemctl status` shows why. A Node.js bot only needs a different `ExecStart=`, such as `/usr/bin/node index.js`.

## Or run it in Docker

If your bots already ship as images, Compose does the same job. One service per bot, a named volume for its data and capped logs:

```yaml
services:
  welcome:
    build: ./welcome
    restart: unless-stopped
    env_file: ./welcome/.env
    volumes:

      - welcome-data:/data

    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  welcome-data:
```

`restart: unless-stopped` brings the bot back after a crash or a reboot, unless you stopped it yourself. Start it with `docker compose up -d` and follow it with `docker compose logs -f welcome`. Keep the `.env` file out of the image and out of version control.

## Keep data through redeploys

A `git pull` or an image rebuild replaces the code folder, so nothing the bot writes should live there. Point its database at `$STATE_DIRECTORY` or the `/data` volume. SQLite is plenty for one bot; move to PostgreSQL when several processes write at once. Back up while the bot runs with SQLite's online backup command, then copy the file off the server:

```bash
sudo sqlite3 /var/lib/bots/welcome/bot.db ".backup '/var/backups/welcome-bot.db'"
```

## Telegram webhooks behind Caddy

Once a Telegram bot needs more than polling can give, switch it to webhooks behind a reverse proxy. Caddy obtains and renews the certificate on its own, as long as the DNS name points at the server and ports 80 and 443 are open:

```text
bot.example.com {
    reverse_proxy 127.0.0.1:8080
}
```

Register the webhook from a root shell (`sudo -i`) so the token comes from the env file instead of your shell history:

```bash
set -a; . /etc/bots/support.env; set +a
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook" \
  --data-urlencode "url=https://bot.example.com/telegram" \
  --data-urlencode "secret_token=${WEBHOOK_SECRET}"
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/getWebhookInfo"
```

Telegram then sends the secret in the `X-Telegram-Bot-Api-Secret-Token` header of every request. Reject any request without it, since anyone can find your URL.

## Updates and monitoring

Deploying a new version is a pull and a restart: `git -C /opt/bots/welcome pull && sudo systemctl restart bot@welcome`. Keep the operating system patched with `unattended-upgrades` on Debian or Ubuntu.

Monitoring needs one extra idea: a running process is not a connected bot. A bot can sit "active" in systemd while its Gateway session is stuck. Have the bot ping a push monitor, such as Uptime Kuma's push type or a Healthchecks check, from inside its event loop every minute, and alert when the pings stop. That catches crashes, hangs and expired tokens with one rule.

## FAQ

### Does a bot need a static IP address?

Not for outbound-only bots. Discord Gateway bots and Telegram polling bots open connections themselves, so any working IPv4 address will do. A Telegram webhook needs a public IPv4 and a DNS name that points to it; a stable address simply avoids downtime while DNS catches up after a change.

### How do I keep the bot token safe?

Store it only in the root-only env file or a secrets store, never in the repository or the image. If it leaks, rotate it at once: reset the token in the Discord Developer Portal, or use the `/revoke` command in BotFather for Telegram, then update the env file and restart the service.

### Why does my Discord bot drop offline for a moment now and then?

Discord occasionally asks clients to reconnect, and network hiccups close WebSockets. Mainstream libraries reconnect and resume the session on their own, so short gaps are normal. If the bot stays offline, check `journalctl` for invalid-session or authentication errors, which usually point to a reset token or a missing intent.

### How many bots fit on the smallest plan?

As many as their combined memory allows. Give each a `MemoryMax=` (or a container memory limit), watch real usage with `systemd-cgtop` for a week, and leave headroom for the operating system and package updates.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in DevOps](https://cloudsat.io/learn/topic/devops-and-automation)

- [Hourly VPS billing explained: what you actually pay for](https://cloudsat.io/learn/hourly-vps-billing-explained): Cloud and hosting Hourly VPS billing explained: what you actually pay for 7 min read
- [Cloud VPS buyer's checklist: what to verify before you pay](https://cloudsat.io/learn/cloud-vps-buyers-checklist): Cloud and hosting Cloud VPS buyer's checklist: what to verify before you pay 7 min read
- [Server migration toolkit: move files, databases and DNS](https://cloudsat.io/learn/server-migration-toolkit): DevOps Server migration toolkit: move files, databases and DNS 8 min read
- [Containerization benefits and costs for small teams](https://cloudsat.io/learn/containerization-benefits-and-costs): DevOps Containerization benefits and costs for small teams 6 min read
