Systemd: create a new service and run on boot

For this occasion, I wanted my telegram bot to start automatically right after my Raspberry Pi Zero booted and connected to the network.

Creating the service file

I named my service file telegram-bot.service:

sudo touch /etc/systemd/system/telegram-bot.service

And started editing it with sudo vim /etc/systemd/system/telegram-bot.service.

I wanted this service to execute python3.6 bot.py on boot, after connecting to the network, under my user (i.e. not as root).

The final form of the file:

[Unit]
Description=Start telegram bot on boot
Wants=network-online.target
After=newtork.target network-online.target

[Service]
User=hlias
Group=hlias
ExecStart=/usr/local/bin/python3.6 bot.py
WorkingDirectory=/home/hlias/my-fetch-bot

[Install]
WantedBy=multi-user.target

Enabling the service

sudo systemctl enable telegram-bot.service

Now, after every reboot, the service will be executed as we wanted.

Leave a comment

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