Setup systemd Service (Production Deployment)¶
This section explains how to configure TG-FileStream using systemd for:
- Automatic startup on boot
- Automatic restart on crash
- Multi-worker support
- Proper process isolation
This guide is based on the recommended production setup.
Service Architecture¶
TG-FileStream uses:
tgfs.service→ Main instancetgfs-worker@.service→ Worker instances (templated) (optional)
Workers run on different ports and sessions.
1. Main Service Configuration¶
Create:
Paste:
[Unit]
Description=TG-FileStream
After=network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/tg-filestream
ExecStart=/home/ubuntu/tg-filestream/venv/bin/python -m tgfs
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Adjust:
UserWorkingDirectory- Project path
2. Worker Template Service¶
[!NOTE]
It is optional and you can skip if you don't want
Create:
Paste:
[Unit]
Description=TG-FileStream Worker
After=network.target
Requires=tgfs.service
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/tg-filestream
ExecStart=/home/ubuntu/tg-filestream/venv/bin/python -m tgfs --port %i --no-update --session %i
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
How Worker Template Works¶
%i is replaced by the instance name.
Example:
This runs:
Each worker:
- Runs on a different port
- Uses separate session
- Can be load balanced by Nginx
3. Reload systemd¶
4. Enable Services¶
Enable main service:
Start main service:
5. Start Worker Instances (If any)¶
Example:
sudo systemctl start tgfs-worker@8081
sudo systemctl start tgfs-worker@8082
sudo systemctl start tgfs-worker@8083
Enable workers at boot:
sudo systemctl enable tgfs-worker@8081
sudo systemctl enable tgfs-worker@8082
sudo systemctl enable tgfs-worker@8083
6. Check Status¶
Main service:
Worker (if any):
7. View Logs¶
Main:
Worker (if any):
Recommended Production Setup¶
Example architecture:
- tgfs.service → Main bot instance
- tgfs-worker@8081
- tgfs-worker@8082
- tgfs-worker@8083
- Nginx load balancing across workers
Restart & Stop Commands¶
Restart main service:
Restart worker:
Stop worker: