how to auto start a program on boot

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.

On Raspberry Pi OS Lite, the easiest solution to start automatically a program on boot is to use the crontab with the @reboot event. On Desktop, the “Desktop Sessions Settings” app can be used to configure 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!

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

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 wrote another post about this 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.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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).
    If you are new to this, I have a guide that you should check, with the most important commands and shortcuts in Nano.

Your task is now scheduled to start 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 create your script under /etc/init.d.
I generally prefer this method for something like a service (I’m using this for my firewall script for example, as explained there).
As I wrote in the introduction, it’s not the best way but if you want to respect the guidelines, but, it works :).

However, there is still 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 myservice 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 the new Raspberry Pi OS versions 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.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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:
  • Save and exit (CTRL+X).

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

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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 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

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

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!)

❤️ Love Raspberry Pi & writing?
Combine your passions and get paid. Write for RaspberryTips!

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.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

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, please leave a message in the community.

You are now ready to create your own scripts and program them on boot. Need more help on the programming side? I have a few articles for you:

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.

Similar Posts

6 Comments

  1. 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.

  2. 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

    1. Hello,

      Yes, they probably work
      Basically, on any Linux system it should work (or at least some of them)

      Patrick

  3. 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.

  4. I forgot to add the line I added as an example:
    sudo python3 /home/pi/webserver/app.pi

  5. 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

Comments are closed.