Get Your Raspberry Pi to Start Automatically (with Service)

Posted by:

|

On:

|

,

I am creating a web server application on a Raspberry Pi using Flask. The server application should automatically start when the Pi powers on. The most common approach is to implement a system service that runs the Python application within a virtual environment. Here’s how to do it.

Part 1: Create shell script

1. Open terminal in Raspberry Pi

2. Create shell script in the home directory.

sudo nano ~/startup_commands.sh

The term “startup_commands” can be replaced with any name meaningful. Note that if you only have one file to execute, you can go straight to part 2 to set the file to run at Startup.

3. Write the commands and save the bash file

#!/bin/bash
cd ~
source myenv/bin/activate
cd Public
python app.py

4. Make the script executable

chmod +x ~/startup_commands.sh

To simplify the process, I like to use WinSCP in my Windows notebook to create startup_commands.sh using note++ and transfer the file to Pi. However, bash file created under Window cannot be run by Linux for 2 reasons. Firstly, Window uses Carriage Return + Line Feed (CRLF), and Linux uses Line Feed (LF). Secondly, Window uses UTF-16 and Linux uses UTF-8 file encoding. To make the bash Script to run properly, I have to clean up the file by removing any unwanted control characters and different file encoding. I open terminal in Raspberry Pi and run the following command:

sed -i 's/\r$//' /home/pi/startup_commands.sh

Explanation of the code:

sed: Stream editor for filtering and transforming text.

-i: Edits the file in place.

s/\r$//: The substitute command (s/):

//: Replaces the matched characters with nothing (effectively removing them).

\r: Matches carriage return characters.

$: Indicates the end of the line.


Part 2: Set the script to run at Startup using Service

1. Create Service File

sudo nano /etc/systemd/system/startup_commands.service

2. Add the configuration into the Service file

[Unit]
Description=Run startup commands

[Service]
WorkingDirectory=/home/pi
ExecStart=bash /home/pi/startup_commands.sh
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

The term “startup_commands” can be replaced with any name. Typically, it includes multiple commands. However, if there is just one file to execute, it can be placed directly at the “ExecStart=” line

For example,

ExecStart=/usr/bin/python3 /home/pi/test.py

Format for ExecStart:

ExecStart = <program language path> <your program path> 

3. Enable service

sudo systemctl enable startup_commands.service

4. Start service

sudo systemctl start startup_commands.service

Difference between enable service and start service:

“enable service” configure the service to start automatically at boot time. While “start service” start a service immediately. “disable service” and “stop service” does the complete opposite respectively.

5. Check if Service is running

sudo systemctl status startup_commands.service 

If errors are found, try to run the scripts following “ExecStart=” directly under terminal and fix all the errors.

6. Reboot Pi and the service will be executed when it starts up.

Everything is set. The Raspberry Pi will be installed in our customer’s server room next week.