Five Ways to Run a Program On Your Raspberry Pi At Startup (2024)

Five Ways to Run a Program On Your Raspberry Pi At Startup (1)Run a Program On Your Raspberry Pi At Startup

In this tutorial we show youfive ways you can run a program on your Raspberry Pi at startup. The five methods that are available to run a program at boot are:

  • rc.local
  • .bashrc
  • init.d tab
  • systemd
  • crontab

Sample Program

You can use any program that you want to run at boot; for this tutorial we are using a sample python program which will speak at the startup of Raspberry Pi. This sample program will use the Espeak package to make the Raspberry pi speak “Welcome to the world of Robots”.

If you do not have the Espeak package installed , run the following in terminal to install:

sudo apt-get install espeak

To learn more about how to get the Raspberry Pi speak, we have a tutorial here.

In the /home/pi directory, open a file for editing:

sudo nano sample.py

And enter the following code and save it (press CTRL+X and enter Y ).

#! /usr/bin/env pythonfrom subprocess import callcall([‘espeak “Welcome to the world of Robots” 2>/dev/null’], shell=True)

Five Ways to Run a Program On Your Raspberry Pi At Startup (2)

Method 1: rc.local

The first method to run a program on your Raspberry Pi at startup is to use the file rc.local. In order to have a command or program run when the Pi boots, you can add commands to the rc.local file. This is especially useful if you want to power upyour Pi inheadless mode (that is without a connected monitor), and have it run a program without configuration or a manual start.

Editing rc.local

On your Pi, edit the file /etc/rc.localusing the editor of your choice. You must edit it with root permissions:

sudo nano /etc/rc.local

Add commands to execute the python program, preferably using absolute referencing of the file location (complete file path are preferred). Be sure to leave the line exit 0 at the end, then save the file and exit. In nano, to exit, type Ctrl-x, and then Y.

Five Ways to Run a Program On Your Raspberry Pi At Startup (3)

If your program runs continuously (runs an infinite loop) or is likely not to exit, you must be sure to fork the process by adding an ampersand (“&”) to the end of the command, like:

sudo python /home/pi/sample.py &

The Pi will run this program at bootup, and before other services are started. If you don’t include the ampersand and if your program runs continuously, the Pi will not complete its boot process. The ampersand allows the command to run in a separate process and continue booting with the main process running.

Now reboot the Pi to test it:

sudo reboot

Hints

Also, be sure to reference absolute file names rather than relative to your home folder. For example use `/home/pi/myscript.py`instead of`myscript.py`.

If you add a script into /etc/rc.local, it is added to the boot sequence. If your code gets stuck then the boot sequence cannot proceed. So be careful as to which code you are trying to run at boot and test the code a couple of times. You can also get the script’s output and error written to a text file (say log.txt) and use it to debug.

sudo python /home/pi/sample.py & > /home/pi/Desktop/log.txt 2>&1

Method 2:.bashrc

The second method to run a program on your Raspberry Pi at startup is to modifythe .bashrc file. With the .bashrc method, your python program will run when you log in (which happens automatically when you boot up and go directly to the desktop) and also every time when a new terminal is opened, or when a new SSH connection is made. Put your command at the bottom of ‘/home/pi/.bashrc’. The program can be aborted with ‘ctrl-c’ while it is running!

sudo nano /home/pi/.bashrc

Go to the last line of the script and add:

echo Running at boot sudo python /home/pi/sample.py

The echo statement above is used to show that the commands in .bashrc file are executed on bootup as well as connecting to bash console.

Five Ways to Run a Program On Your Raspberry Pi At Startup (4)

Now reboot the Pi to hear the Pi speak at startup.

sudo reboot

The below image shows that the commands added to .bashrc file get executed even while opening a new terminal.

Five Ways to Run a Program On Your Raspberry Pi At Startup (5)

Method 3: init.ddirectory

The third method to run a program on your Raspberry Pi at startup is to add the program (to be run on boot) to the /etc/init.d directory. This directory contains the scripts which are started during the boot process (in addition, all programs here are executed when you shutdown or reboot the system).

Add the program to be run at startup to the init.d directory using the following lines:

sudo cp /home/pi/sample.py /etc/init.d/

Move to the init directory and open the sample script

cd /etc/init.dsudo nano sample.py

Add the following lines to the sample script to make it a Linux Standard Base (LSB) (A standard for software system structure, including the filesystem hierarchy used in the Linux operating system) init script.

# /etc/init.d/sample.py### BEGIN INIT INFO# Provides: sample.py# Required-Start: $remote_fs $syslog# Required-Stop: $remote_fs $syslog# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Start daemon at boot time# Description: Enable service provided by daemon.### END INIT INFO

init.d scripts require the above runtime dependencies to be documented so that it is possible to verify the current boot order, the order the boot using these dependencies, and run boot scripts in parallel to speed up the boot process.

You can learn to write init.d scripts following the guide here.

Five Ways to Run a Program On Your Raspberry Pi At Startup (6)

Make the sample script in the init directory executable by changing its permission.

sudo chmod +x sample.py

Run this command:

sudo update-rc.d sample.py defaults

Now reboot to hear the Pi speak at startup.

sudo reboot

Method 4:SYSTEMD

The fourth method to run a program on your Raspberry Pi at startup is to use the systemd files. systemd provides a standard process for controlling what programs run when a Linux system boots up. Note that systemd is available only from the Jessie versions of Raspbian OS.

Step 1– Create A Unit File

Open a sample unit file using the command as shown below:

sudo nano /lib/systemd/system/sample.service

Add in the following text :

 [Unit] Description=My Sample Service After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python /home/pi/sample.py [Install] WantedBy=multi-user.target

You should save and exit the nano editor.

Five Ways to Run a Program On Your Raspberry Pi At Startup (7)

This defines a new service called “Sample Service” and we are requesting that it is launched once the multi-user environment is available. The “ExecStart” parameter is used to specify the command we want to run. The “Type” is set to “idle” to ensure that the ExecStart command is run only when everything else has loaded. Note that the paths are absolute and define the complete location of Python as well as the location of our Python script.

In order to store the script’s text output in a log file you can change the ExecStart line to:

ExecStart=/usr/bin/python /home/pi/sample.py > /home/pi/sample.log 2>&1

The permission on the unit file needs to be set to 644 :

sudo chmod 644 /lib/systemd/system/sample.service

Step 2 – Configure systemd

Now the unit file has been defined we can tell systemd to start it during the boot sequence :

sudo systemctl daemon-reloadsudo systemctl enable sample.service

Reboot the Pi and your custom service should run:

sudo reboot

Five Ways to Run a Program On Your Raspberry Pi At Startup (8)

Method 5:crontab

A detailed tutorial on using crontab to get a program running on boot can found here.

You can use any of these methods to run your program on boot as long as the point at which your Python script is run in the startup sequence is not vital. If your script relies on any system features being available at that point in time such as the network being connected and available, the /home/pi directory is mounted and ready for use or theSystem time has been updated by NTPthen it would be ideal to use either systemd or init.d methods. These methods control the point in bootup at which your script is executed while booting.

Five Ways to Run a Program On Your Raspberry Pi At Startup (9)

Your wish is my command.

See More

Looking to build some awesome projects with the Raspberry Pi and robots? Build your own robot car, the GoPiGo! Check out our projects page for more examples of fun robotics projects with your Raspberry Pi!

Questions

Got a question? Need some help? Ask away on our forums!

Learn More!

If you liked this tutorial, consider getting the Raspberry Pi here and SD Card with Raspbian for Robots here to try out yourself.

Five Ways to Run a Program On Your Raspberry Pi At Startup (2024)

FAQs

Which file is used when Raspberry Pi starts programs automatically start on startup? ›

config/lxsession/LXDE-pi/autostart for our Raspberry Pi. Note that the directory pi might be different if you created a new user for your Raspberry Pi. If no user autostart script is found, Linux will run the global /etc/xdg/lxsession/LXDE-pi/autostart script instead.

How do I make a program run at startup? ›

Add an app to run automatically at startup in Windows 10
  1. Select the Start button and scroll to find the app you want to run at startup.
  2. Right-click the app, select More, and then select Open file location. ...
  3. With the file location open, press the Windows logo key + R, type shell:startup, then select OK.

How do I run a program at startup in Linux? ›

[Basic] How to: Start a Program or Script on Linux Automatically on Boot with systemd
  1. Create the sample script or program that we want to automatically start on boot.
  2. Create a system unit (also known as a service)
  3. Configure your service to automatically start on boot.
22 Jun 2020

What should I do when I first get Raspberry Pi? ›

  1. Setting up your Raspberry Pi. Connecting a Display. SD Cards for Raspberry Pi. Optional items. Troubleshooting.
  2. Installing the Operating System. Using Raspberry Pi Imager. Downloading an Image.
  3. Installing over the Network. Using Network Installation.
  4. Configuration on First Boot.

How do I autorun a Raspberry Pi script? ›

On your Raspberry Pi, edit the file /etc/rc.local as root:
  1. sudo nano /etc/rc.local. ...
  2. python3 /home/pi/myscript.py. ...
  3. /home/pi/schedule.sh. ...
  4. sudo chmod +x /etc/rc.local. ...
  5. sudo reboot. ...
  6. python3 /home/pi/myscript.py & ...
  7. sleep 5. ...
  8. bash -c '/usr/bin/python3 /home/pi/myscript.py > /home/pi/mylog.log 2>&1' &

How do I run a program from the Raspberry Pi from the command line? ›

Re: How to execute installed program
  1. Open up a command line terminal. You will see a command line prompt like: ...
  2. Type the command for the Free Pascal compiler: Code: Select all pi@raspberrypi:~$ fpc. ...
  3. Now that you know it is installed and runs correctly will want to compile your program:
2 Feb 2018

What is program running in startup? ›

A startup program is a program or application that runs automatically after the system has booted up. Startup programs are usually services that run in the background. Services in Windows are analogous to the daemons in Unix and Unix-like operating systems.

What are the steps to run a program? ›

In Windows, to run a program, double-click the executable file or double-click the shortcut icon pointing to the executable file. If you have a hard time double-clicking an icon, you can click the icon once to highlight it and then press the Enter key on the keyboard.

How do you make program run on startup for all users? ›

Open Run command box by pressing Windows logo + R keys. In the Run command field, type shell: startup and then press Enter key to open Startup folder. Copy and paste the app shortcut from the desktop to this Startup folder and the app will be added to startup.

What is startup file in Linux? ›

Loading… Search Results. A startup file is a batch file that is executed automatically each time you start IDL. You can specify a startup file in one of two ways: Specify the name of the startup file in the IDL Workbench from the Window > Preferences > IDL menu option.

How do I run a program in Linux shell? ›

Steps to execute a shell script in Linux
  1. Create a new file called demo.sh using a text editor such as nano or vi in Linux: nano demo.sh.
  2. Add the following code: ...
  3. Set the script executable permission by running chmod command in Linux: chmod +x demo.sh.
  4. Execute a shell script in Linux: ./demo.sh.
11 Aug 2022

How do I run a program from the shell? ›

Steps to write and execute a script
  1. Open the terminal. Go to the directory where you want to create your script.
  2. Create a file with . sh extension.
  3. Write the script in the file using an editor.
  4. Make the script executable with command chmod +x <fileName>.
  5. Run the script using ./<fileName>.

How do I run a script as root on startup? ›

How to use systemd to run a command or script as root on boot. To use systemd to run a command or script as root when your computer boots, create a file (as root) called mycommand. service (replace mycommand with whatever you want to call it) in /etc/systemd/system/ .

How do I run a Python program at startup? ›

The first task in getting a Python program to boot on start-up is to create a script file that call the Python program to execute. Copy and paste the following code into a file and then save that file as “startup.sh”. To keep things simple, make sure that the script file is saved to your Documents folder.

Where is the autostart file in Raspberry Pi? ›

After your desktop environment starts (LXDE-pi, in this case), it runs whatever commands it finds in the profile's autostart script, which is located at /home/pi/. config/lxsession/LXDE-pi/autostart for our Raspberry Pi.

Can you open an application program from start button? ›

The Start menu provides access to every program installed on the computer. To open the Start menu, click the the Start menu button at the bottom-left corner of the screen or press the Windows key on the keyboard.

Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6442

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.