Run a service in Raspberry Pi

Hey there! Let’s talk about running a service on system start in Raspberry Pi. This procedure is important if there are scripts needed to be called during system startup in Raspberry Pi.

Run a service in Raspberry Pi

* How to Run Service on System Start in Raspberry Pi

1. Create a new service file (also called Unit) in /lib/systemd/system/ using command below:

sudo nano /lib/systemd/system/test-service.service

2. Add the following text in the new service file and save it

Description=Test Service
After=multi-user.target
 
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/test-python.py
 
[Install]
WantedBy=multi-user.target

3. Add read/write permission (if needed) to the service file using the following shell command:

sudo chmod 644 /lib/systemd/system/test-service.service

4. Now the service file has been defined, tell systemd to start it during the boot sequence:

sudo systemctl enable test-service.service

5. Reload the systemctl daemon

sudo systemctl daemon-reload

6. To test, reboot the Raspberry Pi:

sudo reboot

7. Check status of service

sudo systemctl status test-service.service

+ Other notes:

a. To disable, run command:

sudo systemctl disable test-service.service

b. Start/Stop/Restart unit file

sudo systemctl start test-service.service
sudo systemctl stop test-service.service
sudo systemctl restart test-service.servicee

Leave a Reply

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

*