How to run Python bot as a service in Ubuntu Linux?

I like building Python bots. Especially for telegram – I think this messenger is very relevant and convenient. All the time of development, debugging and working process of the bots was been on my home server with Windows OS.
But I live and work in Ukraine and fucking russian invaders destroys the energy infrastructure of the country. My UPS is enough for 8 hours of server and network equipment operation in a situation when electricity is down. But I would prefer to spend this charge for other purposes.
I have a great VPS on vultr.com which I decided to move my bots to. So what is needed for this?

See also  Checking the status of network devices with Python and sending messages to Telegram

Create your virtual environment in work folder:

python3 -m venv venv
source venv/bin/activate

Create or upload your script

nano bot.py

Create startup bash script:

nano start.sh

with this content:

cd /home/Bot
source venv/bin/activate
python3 bot.py

Make in executable:

chmod +x /home/Bot/start.sh

Create service file:

nano /lib/systemd/system/Bot.service

with this content:

[Unit]
Description=My Bot
After=multi-user.target
[email protected]

[Service]
Type=simple
ExecStart=/bin/bash /home/Bot/start.sh
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

And run commands:

systemctl daemon-reload
systemctl enable Bot.service 
systemctl start Bot.service 
systemctl status Bot.service

ENJOY!

 

 

Author: admin

1 thought on “How to run Python bot as a service in Ubuntu Linux?

Leave a Reply

Your email address will not be published. Required fields are marked *