zaro

How to Run a Program in Raspberry Pi?

Published in Raspberry Pi Programming 4 mins read

There are several ways to run a program on a Raspberry Pi, depending on your needs and the type of program you want to execute. Here's a breakdown of common methods:

1. Running Programs from the Terminal

The most straightforward method is to execute programs directly from the Raspberry Pi's terminal.

1.1. Navigating to the Program

First, you need to navigate to the directory containing your program. Use the cd command:

cd /path/to/your/program

Replace /path/to/your/program with the actual directory path. For example:

cd /home/pi/my_scripts

1.2. Executing Python Scripts

If your program is a Python script (.py file), you can run it using the python3 command (or python if Python 2 is required).

python3 your_script.py

1.3. Executing Executable Files

If your program is a compiled executable (e.g., a C or C++ program), you can run it directly, provided it has execute permissions:

./your_executable

If you get a "permission denied" error, you need to make the file executable using the chmod command:

chmod +x your_executable

Then try running it again:

./your_executable

2. Running Programs Automatically on Boot

You can configure your Raspberry Pi to run a program automatically when it boots up. This is useful for applications that need to run continuously.

2.1. Using rc.local (Deprecated but sometimes still functional)

Important Note: The rc.local method is deprecated in newer versions of Raspberry Pi OS using systemd. It may or may not work. However, it's worth understanding in case you're using an older OS.

  1. Edit the rc.local file: Open the rc.local file with root privileges using nano:

    sudo nano /etc/rc.local
  2. Add your command: Before the exit 0 line, add the command to execute your program. Crucially, use the full path to your program. This avoids issues with environment variables. For example:

    python3 /home/pi/my_scripts/my_script.py &

    The & symbol runs the program in the background.

  3. Save and Exit: Press Ctrl+X, then Y, then Enter to save and exit nano.

  4. Make rc.local executable: ensure that /etc/rc.local has execute permissions.

    sudo chmod +x /etc/rc.local
  5. Reboot: Reboot your Raspberry Pi to test the changes.

2.2. Using systemd (Recommended for Newer Systems)

systemd is the modern init system used on Raspberry Pi OS. It's the preferred way to start services automatically.

  1. Create a service file: Create a new service file in /etc/systemd/system/. The filename should end with .service. For example, my_script.service:

    sudo nano /etc/systemd/system/my_script.service
  2. Add the service configuration: Add the following content to the service file, modifying it to fit your program:

    [Unit]
    Description=My Script Service
    After=network.target
    
    [Service]
    User=pi
    WorkingDirectory=/home/pi/my_scripts
    ExecStart=/usr/bin/python3 /home/pi/my_scripts/my_script.py
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    • Description: A description of your service.
    • After=network.target: Ensures that the network is up before the service starts.
    • User=pi: Specifies the user to run the service as (usually pi).
    • WorkingDirectory: The directory where the program is located.
    • ExecStart: The command to execute the program, including the full path to the interpreter (e.g., /usr/bin/python3) and the script.
    • Restart=on-failure: Restarts the service if it crashes.
    • WantedBy=multi-user.target: Specifies when the service should start (during multi-user mode).
  3. Save and Exit: Press Ctrl+X, then Y, then Enter to save and exit nano.

  4. Enable the service: Enable the service so that it starts on boot:

    sudo systemctl enable my_script.service
  5. Start the service: Start the service manually to test it:

    sudo systemctl start my_script.service
  6. Check the service status: Check the status of the service to ensure it's running correctly:

    sudo systemctl status my_script.service
  7. Reboot: Reboot your Raspberry Pi to confirm that the service starts automatically.

3. Using a Graphical Interface (GUI)

If you're using a Raspberry Pi with a desktop environment, you can create shortcuts or use the task scheduler to run programs. These methods are more GUI-focused and depend on the specific desktop environment you're using.

Summary

Running programs on a Raspberry Pi involves using the terminal for direct execution or configuring the system to run programs automatically on boot using rc.local (older systems) or, preferably, systemd (newer systems). Remember to use full paths to your programs and ensure proper permissions.