How to Auto Start a Program on Raspberry Pi? (4 ways)


Starting a script on boot is not really intuitive, whatever your system.
It’s a question I often hear, so I created an entire post about it.
You’re probably trying to start a script or an app, that’s mandatory in your setup, automatically on boot.
It’s not so complicated, you have several ways, I’ll show you everything.

There are several solutions to automatically start a script or program on boot.
On Raspberry Pi OS Lite, the easiest one is to use the crontab with the @reboot event.
On Desktop, the “Desktop Sessions Settings” app can be used to do the same thing
.

So yes, it’s possible, but you need to find the solution that fits your needs.
You can also choose to follow “the good practices”, or keep only the simplest solution to remember.
That’s often my favorite choice, I don’t care about good practices when I’m the only one to use the device.
Let’s go!

By the way, if you don’t have a budget to afford a course for now (if you just got your Raspberry Pi stuff, I completely understand), you can join Skillshare for a free 14-day trial. It includes courses from the best teachers in many categories, including IT (Linux, programming, …) and Raspberry Pi. I highly recommend getting this offer now, so that you can already learn a bunch of things in the next fortnight. I’m a fan of their services personally 🙂

How to auto start a program

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/pi/Desktop/test.sh.

You need to change this line with the script or program you want to run.
If you have no idea which command you need to type, check the second part of this guide :).

1 – Use the crontab

Cron is a service, automatically started at each boot of the Raspberry Pi, which allows the user to execute scheduled commands.
A crontab is a file that will allow us to list what we want to start and when to start it, in a format understandable by the cron service.

The crontab has many options to start a script at a specific time or regularly (daily, weekly, 3 times a month, etc.).
By the way, I have a post about this specifically that you can check to learn more.
But that’s not what we are looking for.

The crontab also adds the possibility to start a script on boot, with the @reboot option.
That’s the first solution you can use to start your program automatically, and probably the easiest one to remember in this list.

Here is how to do this:

  • 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 get an empty crontab file, it looks like this:
  • Paste a line starting with reboot, and add your script command just after, like this:
    @reboot /home/pi/Desktop/test.sh
  • Save and exit (CTRL+O, CTRL+X with nano).

Your task is now scheduled to start at each boot.

Raspberry Pi Bootcamp
Sale: 10% off today.
Take it to the next level.
I'm here to help you get started on Raspberry Pi.
Learn all the skills you need in the correct order.

2 – Put your script in /etc/init.d

The second solution you can use is to create your script under /etc/init.d.
I generally prefer this method for something like a service (like a firewall script for example).
As I wrote in the introduction, it’s not the best way but if you respect the guidelines, it works :).

However, there is a minimal format to respect for the script to start it on boot.
Here 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/pi/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 is doing.
  • Save your file and exit (CTRL+X).
  • Add the execution permission to your script:
    sudo chmod +x /etc/init.d/myservice
  • Finally, you need to tell the system to start it on boot by using this command:
    sudo update-rc.d myscript defaults

That’s really the minimum to make it work.
The goal here is just to run a script on boot.
If you want to do this properly, you need to add at least start, stop and restart option to your service.
You can find a detailed documentation here if that’s what you want to do.

Reboot to try, if all is correct your script should run once every time you start your Raspberry Pi.

3 – Create an upstart job

The third option is to create an upstart job.
In the previous Raspberry Pi OS version, it was possible to handle this graphically, but on Raspberry Pi OS Buster it seems to be unavailable.
Let me know if you find a solution on Raspberry Pi OS Desktop.

So, here is how to do this with a configuration file:

  • Create a file in /etc/init (not init.d!):
    sudo nano /etc/init/myjob.conf
  • Paste the following content:
description "my job"
start on startup
task
exec /home/pi/Desktop/test.sh
  • Don’t forget to adapt the file name and description to remember what it is :).

That’s all!
Just creating this file is enough to make it work at each boot.

Bonus:
There is no graphical way to do this, but you can manage which upstart job you want to run on next boot:

  • Go in the Main Menu > Parameters > Main Menu Editor.
  • Browse to Main Menu > Parameters and check the “Desktop Session Settings” box:
  • Close this window and go back to Main Menu > Parameters, then open Desktop Session Settings:
  • Here you can check which upstart job is configured, and enable or disable them.

4 – Add a line in /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 your script or service just before the “exit 0” line
    Something like this:
    rc.local configuration file
  • Save and exit (CTRL+X).

That’s already done, nothing else to do.
You can reboot now to try it if you want.

Find the command to start

Before trying the previous solutions to auto-start a program on boot, you may need some help to know exactly what to run.
Sadly, Raspberry Pi OS Buster doesn’t include a tool to start apps on boot, so you have to find the corresponding command line.
As I don’t know exactly what you want to run, I will give you the three options.

Create a script

If your goal is to create a script, the command line will be easy to find, it’s the path and the script name.

For example, if your script is in /usr/local/bin and the script name is start_script.sh.
You can use the following command in one of the 4 previous solutions to start your script on boot:
/usr/local/bin/start_script.sh

Don’t forget to add the execution permission with:
chmod +x /usr/local/bin/start_script.sh

Master your Raspberry Pi in 30 days
Sale: 10% off today.
Download the eBook.
Uncover the secrets of the Raspberry Pi in a 30 days challenge.
Learn useful Linux skills and practice multiples projects.

Use a command line

The second option is to run a command on boot.
This is also an easy solution if you know exactly what you want to.
Issues may come from the command path.

Each command (htop, nano, mail, etc.) is installed at a specific location on your system (/usr/local/bin, /usr/bin, etc.).
Most of the commands will work directly on boot, without any path indication.
But for some programs, often less known, GitHub projects or things like that (I’m thinking about AlexaPi for example), it’s not the case, you have to give the complete path.

If you have no idea what the executable location is, here are a few tips
You can use the “which” command to find it:
which <COMMAND>

Here is an example:

Knowing that you can use “/usr/bin/php” in your configuration file.

If it doesn’t work, the “find” command may also help you:
sudo find / -iname php

Find the program command

My last tip is for the graphical program that you run directly from the main menu.
As you don’t know the corresponding command line, it’s difficult to start it automatically.
But there is an easy way to find 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.

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.

Conclusion

That’s it, you now know 4 solutions to automatically start a program on boot.
Make your choice depending on what your program is, and maybe the method you remember most easily.

You can also use other solutions like systemd, check the Debian wiki if you want to know more about this.

If you know other methods, easier or not, I’d like to see them in the comments below!


This tutorial doesn't work anymore? Report the issue here, so that I can update it!


Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts

Raspberry Pi Bootcamp - 10% off