How to Auto Start a Program on Raspberry Pi? (4 Ways)
You may want to launch a script or program on boot, but you don’t see an option for it on Raspberry Pi OS. It’s a question I often hear about, and the methods are not intuitive. In this post, I’ll show you several ways to accomplish this goal.
On Raspberry Pi OS, the easiest solution to start automatically a program on boot is to use the crontab with the @reboot event. There are also other methods using the Linux init systems.
So yes, it’s possible, but you’ll need to go with the solution that best fits your needs. You can choose to follow the “best practices” or just remember the simplest solution (that’s often my favorite). Let’s go!
If you’re new to Raspberry Pi or Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your Raspberry Pi. Click here to get it for free!
Methods to Run a Program at Startup
In this first part, I’ll go directly to the main point: how to start any script or program on boot.
For the example, I’ll use my test script which is in the Desktop folder, so /home/pat/Desktop/test.sh.
You’ll need to change this line to the script or program you want to run. If you have no idea which command you need to input, read the second part of this guide :).
1 – Use Crontab
Cron is a service automatically started at boot which allows the user to run scheduled tasks. A crontab is a file that will allow us to list what we want to execute and when to start it, in a format understandable by the cron service.
The crontab has many settings to start a script at a specific time or schedule (daily, weekly, 3 times a month, etc.). I wrote another post about scheduling tasks that you can check out to learn more.
The crontab also gives us the option to launch a program on boot, with the @reboot option.
This is our first solution to starting a program automatically, and it’s probably the easiest one to remember from this list.
Here’s how to launch a program at boot with crontab:
- Open a terminal.
- Open the crontab file:
crontab -e
If’s your first time in the crontab, you need to select an editor (press Enter for nano). - You’ll start with an empty crontab file that looks like this:
- Add a line starting with @reboot, followed by your script command, like this:
@reboot /home/pat/Desktop/test.sh
- Save and exit (CTRL+O, CTRL+X with nano).
(If you’re new to this, I have a guide that shows you the most important commands and shortcuts in Nano.)
Your command is now scheduled to launch at each boot.
Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.
2 – Put Your Script in /etc/init.d
The second solution you can use is to place your script in the /etc/init.d directory.
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
I generally prefer this method for something like a service (like for my firewall script, as explained here). As I mentioned earlier, it’s not the best practice if you want to respect the Linux guidelines, but it works :).
However, there’s still a format to follow for it to work correctly on boot.
Here’s how to do this:
- Create a new file in /etc/init.d:
sudo nano /etc/init.d/myservice
Change the file name to make it explicit. - Paste these lines (for example):
#!/bin/bash
### BEGIN INIT INFO
# Provides: MyService
# Required-Start: $all
# Required-Stop:
# Default-Start: 5
# Default-Stop: 6
# Short-Description: Your service description
### END INIT INFO
touch /home/pat/myservice_test
- The comments at the beginning are mandatory to make it start on boot (runlevel 5).
Change the service name and description to explain what your script should do. - Save & exit (CTRL+X).
- Add the execution permission to your script:
sudo chmod +x /etc/init.d/myservice
- Finally, tell the system to start it on boot by using this command:
sudo update-rc.d myservice defaults
That’s the minimum to make it work. The goal here is just to run a script on boot. Reboot to try. If all is correct, your script should run once every time you start your Raspberry Pi.
If you want to set this up more properly, you can add start, stop and restart options to your service. You can find a detailed documentation here if that’s what you want to do.
3 – Create a Systemd Service
The third option is to create a systemd service.
Some might consider this the most proper way. It lets you control your program using the ‘systemctl’ command, just like you would for other services like VNC, Nginx, or cloudflared. However, it does have a specific format you need to follow below.
Here’s how to create a systemd service that runs on startup:
- Create a file in /lib/systemd/system:
sudo nano /lib/systemd/system/mytest.service
- Paste the following lines:
[Unit]
Description=MyService
After=multi-user.target
[Service]
Type=idle
ExecStart=/home/pat/Desktop/test.sh
[Install]
WantedBy=multi-user.target
- Change ExecStart to the full path to your command, and Description to remember what it is.
- Save & exit (CTRL+X).
- Modify the service file’s permissions to match other system services:
sudo chmod 644 /lib/systemd/system/mytest.service
- Finally, tell systemd to run it at boot:
sudo systemctl enable mytest.service
That’s all! When you reboot, your program should run after all key system services have started.
4 – Add a Line to /etc/rc.local
Finally, the rc.local method is probably the easiest as you just need to remember the file name to know what to do :).
The rc.local file is a script intended for the system administrator (you!).
On boot, it’s one of the last things executed (at the end of the multi-user run level).
So, it’s exactly what you need to run a custom service.
Here is how to use it:
- Open the rc.local file:
sudo nano /etc/rc.local
- Insert script or service just before the “exit 0” line
Something like this: - Don’t forget the ampersand(&) symbol at the end, or it might not work!
- Save and exit (CTRL+X).
You’re done—there’s nothing else to do. You can reboot now to try it out if you want.
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
Finding the Command to Auto-Start
Before trying to auto-start a program on boot, you may need some help knowing exactly what to run.
Sadly, Raspberry Pi OS doesn’t include a tool to start apps on boot, so you’ll have to find the command that launches the program. In this section, I’ll go over three ways to find it.
Provide Your Script Path
If your goal is to run a script on boot, the command is easy to find: it’s the path to the script.
For example, let’s say you created a script that resides in /usr/local/bin with the name start_script.sh.
For it to run boot, make sure that your script is executable. You can add the permission with:chmod +x /usr/local/bin/start_script.sh
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
Now you can input this command into one of the solutions above to run on boot:/usr/local/bin/start_script.sh
Find the Executable
A second option is to run an executable command on boot. This is an easy solution if you know exactly what you want to do. The only difficulty you might run into involves the path.
Each command (htop, nano, mail, etc.) is installed at a specific location on your system (/usr/local/bin, /usr/bin, etc.). Most system commands will run properly on boot even if you don’t specify a path.
But for some programs, like lesser-known GitHub projects or things like that (I’m thinking about Log2Ram for example), this is not the case. Instead, you have to provide the complete path.
If you have no idea where the executable location is, here are a few tips.
You can use the “which” command to find it:which <COMMAND>
Here’s an example:
Now that you’ve found the path, you can input “/usr/bin/php” in your autostart file.
If “which” didn’t work, the “find” command may be helpful to find a program:sudo find / -iname php
If you need more details about this, you should probably read my other article here: 3 Commands to Search For a File on Raspberry Pi (and find it!)
View the Menu Shortcut
My last method works for graphical programs that you run directly from the Raspberry Pi OS main menu. Since you can’t see the shortcut’s location, it’s difficult to find the executable file.
But there’s an easy way to locate it:
- In the Main Menu, go to Preferences > Main Menu Editor.
- Select the application you want to start.
There are sorted the same way as on the main menu, so browse into the main categories if needed. - Then click on “Properties” on the right.
- A window shows up with the launcher properties.
In the Command field, you can see the exact command used when you click on it in the main menu.
Now you have everything you need to apply one of the four solutions we discussed earlier.
If you need the path location, check my previous tip with the which command.
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.
Conclusion
That’s it—you now know four ways to automatically start a program on boot. Choose your preferred solution depending on what your program is, or maybe just choose the method you remember most easily.
If you know of other methods, easier or not, I’d like to know about them. Please leave a message in the community.
You’re now ready to create your own scripts and program them to run on boot! If you need more help on the programming side of things, I have a few articles waiting for you:
- The 7 Best Text Editors for Programming on Raspberry Pi
- Should You Learn Linux or Python first?
- 15 Fun Raspberry Pi Projects to Experiment with Python
Whenever you’re ready, here are other ways I can help you:
The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.
You can also find all my recommendations for tools and hardware on this page.
Patrick,
Here I am again with a different problem!
I tried the Upstart method for x11vnc (server app to allow me to control my Pi from my Desktop).
I did:
sudo nano /etc/init/x11vnc
and set contents as:
description “x11vnc”
start on startup
task
exec x11vnc -gui tray-setpass -rfbport PROMPT -bg -o %%HOME/.x11vnc.log.%%VNCDISPLAY
The exec command’s parameter was obtained from the Main Editor’s Properties entry for x11vnc. I rebooted and x11vnc was not autostarted. I checked the Automatically Started Applications in Desktop Session Settings and there was no entry for x11vnc (there were other entries present e.g. xcompmgr). I’ve tried putting ” around the exec parameter but it made no difference. Interestingly when I installed the clipit package (for a clipboard manager), that was automatically created in the Automatically Started Applications and does start at startup.
i was wondering if these methods also apply to armbian on the orange pi. one would assume so, or at least some itteration of, seeing as they are both linux/debian-based. thanks for the refresher. sometimes even the simplest of things are lost to time and memory
Hello,
Yes, they probably work
Basically, on any Linux system it should work (or at least some of them)
Patrick
What about adding a line into bachrc?
I saw this in a youtube video and it seems to work. I know nothing about linux though and I have no idea how to stop it once it’s started in this way. In my case I’m starting a Python script that runs a webserver.
I forgot to add the line I added as an example:
sudo python3 /home/pi/webserver/app.pi
is it possible to start a GUI app (calibre)
I tried the above methods but still no success.
If anybody could guide me in the correct direction it would be immensely helpful.
Thanks