Closed derdide closed 1 month ago
This will allow the bot to run automatically on startup and restart if it crashes. Here's a step-by-step guide to achieve this:
First, we'll create a service file for your bot. Let's call it telegrambot.service
.
[Unit]
Description=Telegram Bot Service
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/your_bot_script.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Save this file:
sudo nano /etc/systemd/system/telegrambot.service
Paste the content from the artifact above into this file, save and exit (Ctrl+X, then Y, then Enter).
Modify the service file:
/home/pi/your_bot_script.py
with the actual path to your bot script.WorkingDirectory
accordingly.Set the correct permissions:
sudo chmod 644 /etc/systemd/system/telegrambot.service
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
Enable the service to start on boot:
sudo systemctl enable telegrambot.service
Start the service:
sudo systemctl start telegrambot.service
Check the status of your service:
sudo systemctl status telegrambot.service
Now your bot should be running as a service. It will start automatically when your Raspberry Pi boots up, and it will restart if it crashes for any reason.
Additional commands that might be useful:
sudo systemctl stop telegrambot.service
sudo systemctl restart telegrambot.service
sudo journalctl -u telegrambot.service
As this bot is supposed to run on a server, making it a service will allow to have it run permanently, and restarted after reboots or crashes. This is mostly (only?) a documentation question