PDF download Download Article PDF download Download Article

Whether it be used as a media center, web server, gaming console, or simply a computer, there are countless reasons why you might need a particular program or script to automatically start when you boot your Raspberry Pi. This wikiHow teaches you how to force a script to start at boot time by either adding it as a service (before the desktop loads), or by adding it to your autostart file (after the desktop loads).

Method 1
Method 1 of 2:

Adding a Script as a Service

PDF download Download Article
  1. This method will teach you how to create a service that runs your script at boot time before LXDE starts. If the script requires dependencies that aren't active immediately (such as networking or anything graphical), the service will tell your script to wait until those processes load. Create a file (called scriptname.service ) that follows this structure, replacing the paths and description as needed:

    [Unit]

    Description=My Service

    After=network-online.target

    [Service]

    ExecStart=/usr/bin/python3 -u main.py

    WorkingDirectory=/home/pi/myscript

    StandardOutput=inherit

    StandardError=inherit

    Restart=always

    User=pi

    [Install]

    WantedBy=multi-user.target

  2. You'll need to do this as root, so use sudo . Here's an example:
    • sudo cp myscript.service /etc/systemd/system/myscript.service
    Advertisement
  3. Before setting the service to start automatically, you'll want to make sure the script runs properly when executed. To run the script through the service, you'll use the systemctl command like this:
    • sudo systemctl start myscript.service
    • You can stop the service if necessary by running the same command, replacing start with stop .
  4. As long as the script executes without error, you can run the command that ensures the script runs at boot time:
    • sudo systemctl enable myscript.service
  5. Advertisement
Method 2
Method 2 of 2:

Using the Autostart File

PDF download Download Article
  1. This method requires the PIXEL desktop environment with the latest version of the Raspbian operating system. It is achieved by editing an "autostart" file that runs commands at startup in the LXDE desktop environment, which is the framework the Raspberry Pi's PIXEL desktop is built on. This method is preferred in scenarios where:
    • Your script requires the desktop environment to run
    • Your script needs to run from a terminal window
    • You want to specify which users the script will autostart for
  2. In a new terminal window, run the following command: [1]
    • sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
  3. This will create a new line above the @xscreensaver -no-splash entry. Many users have reported that commands added below this line do not run successfully so it's common practice to add your custom commands above the @xscreensaver entry.
  4. This is where you have the most options as you can enter any terminal command you would normally use to launch your script. For example, to run a bash script called "myscript.sh" which is located in the home directory of the user "pi" one would enter the command: /home/pi/myscript.sh . Likewise, if superuser permissions are required to run the script you can even preface the command with sudo .
  5. 5
    Include the full paths to every filename. For example, to run a Python script that requires superuser permissions called "myscript.py" which is located in the home directory of the user "pi", you would enter the full path to both the Python script itself and the Python interpreter, such as sudo /usr/bin/python /home/pi/myscript.py .
  6. For this, you can use the @lxterminal command with the -e modifier followed by the full path of your script in quotes. For example:

      @lxterminal -e "/home/pi/myscript.sh"

    • This will execute the script from a new terminal window.
  7. Once entered, press Ctrl + X , and then follow the on-screen instructions to save your changes.
    • If you don't want your script to run on startup any longer, simply edit the autostart file once again, only this time delete the lines you added previously. Once you save and exit, your script will no longer run automatically on startup.
  8. Use sudo reboot to reboot your Raspberry Pi. If the script works as designed, it will launch one the window manager starts up.
  9. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Article Summary X

      1. Create a .service file for your script.
      2. Copy the file to /etc/systemd/system.
      3. Test the script by starting the service.
      4. Use systemctl to launch at boot time.

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 154,504 times.

      Is this article up to date?

      Advertisement