Setting Up a VDS for Telegram Bots and Automation Scripts

Setting Up a VDS for Telegram Bots and Automation Scripts

Run your Telegram bot and scheduled scripts 24/7 on a VDS. Python virtual environments, systemd timers, cron, Docker isolation and log management.

White Bilişim

Setting Up a VDS for Telegram Bots and Automation Scripts

A Telegram bot, a scheduled data collection script, or a small service that sends notifications: they all share one requirement, uninterrupted uptime. This guide walks through setting these workloads up properly on a VDS.

1. Server Preparation and Security

sudo apt update && sudo apt upgrade -y
sudo adduser --disabled-password --gecos "" bot
sudo apt install -y python3-venv python3-pip git

Log in over SSH with a key, disable password authentication, and keep only the SSH port open in the firewall:

sudo ufw allow OpenSSH
sudo ufw enable

💡 Bots make outbound connections, so you do not need to open a port for incoming traffic. Keeping the number of open ports to a minimum is the single most effective security measure.

2. Python Virtual Environment

Do not pollute the system Python; create a separate virtual environment for each project:

sudo su - bot
git clone https://github.com/username/telegram-bot.git ~/bot
cd ~/bot
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

3. Storing the Token Safely

echo "TELEGRAM_TOKEN=..." > ~/bot/.env
chmod 600 ~/bot/.env

⚠️ Never hardcode the token or push it to a repository. Tokens that end up in a public repository by accident get scanned and abused within minutes; if that happens, regenerate it immediately through BotFather.

4. Keeping It Running With systemd

[Unit]
Description=Telegram Bot
After=network.target
 
[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/bot
EnvironmentFile=/home/bot/bot/.env
ExecStart=/home/bot/bot/.venv/bin/python main.py
Restart=always
RestartSec=10
 
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now telegram-bot
sudo journalctl -u telegram-bot -f

5. Scheduled Tasks: cron or systemd Timer?

Featurecronsystemd timer
Ease of setupSimpleSlightly more work
Log managementManualBuilt in via journald
Missed runsSkippedCaught up with Persistent=true
Dependency managementNoneService dependencies can be declared

For simple periodic jobs, cron is enough:

*/15 * * * * /home/bot/bot/.venv/bin/python /home/bot/bot/task.py

If missed runs need to be caught up after the server has been down, go with a systemd timer.

6. Isolation With Docker

If you run several bots with conflicting version requirements, keeping each one in its own container is the cleanest approach:

docker run -d --name telegram-bot \
  --env-file /home/bot/bot/.env \
  --restart unless-stopped \
  my-bot:latest

--restart unless-stopped makes sure the container comes back up on its own after a server reboot.

7. Log and Disk Management

Log files fill a disk quietly. If you are using systemd, cap the journald size:

SystemMaxUse=500M

If you write your own log files, set up logrotate. Otherwise, a few months from now, you will be troubleshooting a bot that stopped because the disk filled up.

8. Resource Planning

Bot typeTypical memory
Text commands150-400 MB
Image processing bot1 GB+
Database backed bot+200-500 MB
Web scraping / crawler500 MB - 2 GB

Conclusion

Moving automation work onto a server does not require complex infrastructure: a virtual environment, a process manager and regular log rotation cover it. The only remaining requirement is a server that stays up.

👉 For a package that fits your workload, see our Telegram bot and automation VDS page, and if you also run bots on Discord, take a look at Discord bot VDS.