0
0
Raspberry Piprogramming~30 mins

systemd service for auto-start in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a systemd Service for Auto-Start on Raspberry Pi
📖 Scenario: You want your Raspberry Pi to automatically run a simple script every time it boots up. This is useful for projects like home automation, data logging, or any task that should start without manual intervention.
🎯 Goal: Build a systemd service that runs a custom script automatically when the Raspberry Pi starts.
📋 What You'll Learn
Create a simple script file that prints a message to a log file
Create a systemd service file with the correct configuration
Enable the service to start on boot
Verify the service runs and outputs the expected message
💡 Why This Matters
🌍 Real World
Many Raspberry Pi projects need to run tasks automatically when the device powers on, such as starting servers, sensors, or automation scripts.
💼 Career
Understanding systemd services is important for Linux system administration, DevOps roles, and embedded systems development.
Progress0 / 4 steps
1
Create the script file
Create a file called /home/pi/startup_script.sh that writes the text "Raspberry Pi started" to /home/pi/startup.log. Use the exact command echo "Raspberry Pi started" >> /home/pi/startup.log inside the script.
Raspberry Pi
Need a hint?

Use a text editor like nano or vim to create the script file. Don't forget to add the #!/bin/bash line at the top.

2
Create the systemd service file
Create a file called /etc/systemd/system/startup.service with these exact contents:
[Unit]
Description=Startup Script Service
After=network.target

[Service]
Type=simple
ExecStart=/home/pi/startup_script.sh

[Install]
WantedBy=multi-user.target
Raspberry Pi
Need a hint?

Use sudo nano /etc/systemd/system/startup.service to create the service file. Make sure the file has the exact sections and lines.

3
Enable and start the systemd service
Run these exact commands to enable the service to start on boot and start it now:
sudo systemctl enable startup.service
sudo systemctl start startup.service
Raspberry Pi
Need a hint?

Use sudo because these commands need administrator rights.

4
Check the output log
Run cat /home/pi/startup.log to display the contents of the log file and verify it contains the text Raspberry Pi started.
Raspberry Pi
Need a hint?

If you see the text Raspberry Pi started, the service worked correctly.