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

1. Using Systemd: Systemd is a system and service manager for Linux operating systems. It is used to manage system services and start programs at boot. To auto start a program on Raspberry Pi, you can create a systemd service file and enable it.

2. Using Crontab: Crontab is a Linux utility used to schedule tasks to be executed periodically. You can use crontab to auto start a program on Raspberry Pi at a specific time.

3. Using rc.local: rc.local is a script that is executed at the end of the boot process. You can use rc.local to auto start a program on Raspberry Pi.

4. Using Autostart: Autostart is a graphical tool used to configure programs to start automatically when the Raspberry Pi boots. You can use Autostart to auto start a program on Raspberry Pi.

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.

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.

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.

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

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.

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:

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? 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.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

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

Raspberry Pi is a great tool for running programs and applications. But what if you want to have a program or application start automatically when you boot up your Raspberry Pi? Here are four ways to auto start a program on Raspberry Pi.

1. Using Systemd

Systemd is a system and service manager for Linux operating systems. It is used to manage the services, devices, and the rest of the system. To auto start a program on Raspberry Pi using systemd, you need to create a systemd service file. This file will contain the instructions for systemd to start the program when the Raspberry Pi boots up.

2. Using Crontab

Crontab is a time-based job scheduler in Linux. It is used to schedule commands or scripts to run periodically. To auto start a program on Raspberry Pi using crontab, you need to edit the crontab file and add the command to start the program. The command will be executed at the specified time.

3. Using Autostart

Autostart is a program that is used to automatically start applications when the Raspberry Pi boots up. To auto start a program on Raspberry Pi using Autostart, you need to create a .desktop file in the autostart directory. This file will contain the instructions for Autostart to start the program when the Raspberry Pi boots up.

4. Using rc.local

rc.local is a script that is executed at the end of the boot process. To auto start a program on Raspberry Pi using rc.local, you need to edit the rc.local file and add the command to start the program. The command will be executed at the end of the boot process.

These are four ways to auto start a program on Raspberry Pi. Each method has its own advantages and disadvantages, so it is important to choose the one that best suits your needs.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?