How to Create an Auto-Startup Script on a Raspberry Pi
- Solomon Ankomah
- How to
- 08 Dec, 2022
Setting up a Raspberry Pi to run a program automatically on startup is a common requirement for projects like IoT devices, media servers, or kiosks. This guide walks you through different methods to create an auto-startup script on a Raspberry Pi running Raspberry Pi OS (formerly Raspbian).
Prerequisites
Before getting started, ensure you have the following:
- A Raspberry Pi running Raspberry Pi OS.
- Access to the Raspberry Pi via SSH or a connected monitor and keyboard.
- A program or script (e.g., Python, shell script) that you want to run on startup.
- Basic familiarity with the Linux command line.
Method 1: Using rc.local
rc.local is a legacy initialization file that runs commands at the end of the boot process.
Steps:
-
Open the
rc.localfile for editing:sudo nano /etc/rc.local -
Add the command to run your script before the
exit 0line:/usr/bin/python3 /home/pi/my_script.py &- Replace
/usr/bin/python3with the interpreter for your script (e.g., Python). - Replace
/home/pi/my_script.pywith the full path to your script. - The
&at the end runs the script in the background.
- Replace
-
Save the file and exit (Ctrl+O, Enter, Ctrl+X).
-
Make the script executable (if not already):
chmod +x /home/pi/my_script.py -
Reboot to test:
sudo reboot
Method 2: Using cron
The cron scheduler can be used to run tasks at specific times, including system startup.
Steps:
-
Edit the
crontabfile:crontab -e -
Add the following line to run your script at boot:
@reboot /usr/bin/python3 /home/pi/my_script.py -
Save and exit the editor (e.g., for
nano, Ctrl+O, Enter, Ctrl+X). -
Make the script executable (if not already):
chmod +x /home/pi/my_script.py -
Reboot to test:
sudo reboot
Method 3: Using systemd
systemd is the default init system on Raspberry Pi OS. It provides more robust control over services and startup scripts.
Steps:
-
Create a new service file:
sudo nano /etc/systemd/system/my_script.service -
Add the following configuration:
[Unit] Description=My Startup Script After=network.target [Service] ExecStart=/usr/bin/python3 /home/pi/my_script.py Restart=always User=pi [Install] WantedBy=multi-user.target- Replace
/usr/bin/python3and/home/pi/my_script.pywith the appropriate paths. Restart=alwaysensures the script restarts if it crashes.
- Replace
-
Save and exit (Ctrl+O, Enter, Ctrl+X).
-
Enable the service:
sudo systemctl enable my_script.service -
Start the service immediately (optional):
sudo systemctl start my_script.service -
Check the service status:
sudo systemctl status my_script.service -
Reboot to confirm it runs on startup:
sudo reboot
Method 4: Using the Desktop Environment (For GUI Applications)
If you’re running a graphical interface and want to start a program when the desktop environment loads:
Steps:
-
Open the autostart configuration:
nano ~/.config/lxsession/LXDE-pi/autostart -
Add your command at the end of the file:
@/usr/bin/python3 /home/pi/my_script.py -
Save and exit (Ctrl+O, Enter, Ctrl+X).
-
Reboot to test:
sudo reboot
Debugging Tips
-
Check Logs: If the script doesn’t run as expected, check the system logs:
journalctl -xe -
Script Permissions: Ensure the script is executable:
chmod +x /path/to/your_script -
Script Path: Use absolute paths for all files and commands in your script.
-
Dependencies: Ensure all required libraries or dependencies are installed and accessible to the user running the script.
Conclusion
With these methods, you can easily configure your Raspberry Pi to run a script automatically on startup. Choose the method that best fits your project requirements, and enjoy the convenience of having your Raspberry Pi ready to go as soon as it powers on.