Execute Script on Startup Thumbnail

Execute Script on Startup [Raspberry Pi]


Getting your script to execute when you boot up your Raspberry Pi can be very useful. For example, connecting your Raspberry pi to your Synergy server automatically when it turns on so you don’t need to plug in a keyboard just to execute the “startsynergy” command.

We will focus our setup around the Raspbian OS. Even though the setup is different between the Raspbian GUI OS and the Headless OS (Raspbian Lite), the processes is still fairly straight forward. This guide will demonstrate how to do it on both Operating Systems.

Raspbian Lite (Headless OS)

I will start off with the easiest of the lot. As you boot up into a terminal in an headless environment, we can use the .bashrc file of the user you are logging in as.

First ensure that the file exists (it should by default… but just encase…)

> sudo touch ~/.bashrc

This will create (if not already created) a .bashrc file within the currently logged in users home directory. Next we want to edit this file using the Nano Text Editor:

> sudo nano ~/.bashrc

With our .bashrc file open, scroll all the way to the bottom of the file using your keyboards arrow keys. On the last line of the file (but before any exit statements) type the script you want to execute in the exact same way as you would in the terminal. For example, if I wanted to execute a python script, I enter the following

python ~/Scripts/hello.py

Notice how I am using an absolute path to the file (~/ is a shortcut to the home directory of the logged in user). It is best to stick to absolute paths for this kind of stuff.

When you’re done, press “CTRL + X” to exit and “Y” to confirm changes. Reboot your Pi and you should see your script execute!

Caveat: One thing to note is, this script will run every time you open up a terminal. So if you log into your Pi via SSH from your main PC, this script will run again. If you have a long running script, you can press “CTRL + C” to break out of it early.

Raspbian GUI

Executing a script on startup with the Raspbian GUI is slightly different. The script has to be executed when the desktop environment loads. We can edit the file which runs when this event occurs by opening it with Nano:

> sudo nano /etc/xdg/lxsession/LXDE/autostart

Like our .bashrc file, we must add our script to the end of this file (but again ensuring we put it before any exit statements). For this example, I will use our startsynergy script we created some posts ago. It is launched via bash, so the line would look as follows:

@/usr/bin/bash ~/startsynergy

Upon restarting the Pi, and providing you have your Synergy Server running, your Pi should now auto connect to your Synergy Server without the need for manual intervention.

Likewise, if you want to execute python scripts, for example, you can do so in the following manner:

@/usr/bin/python /path/to/my/python/file.py

To save the file hit “CTRL + X” and “Y” to confirm changes. Upon rebooting your Pi and logging into the desktop, you should notice that your script executes.

Published by


Nick Cullen is a software developer living in South Wales, UK. He is primarily focused around coding in C++ and C# and loves tinkering with new programming languages and technologies. A key technological interest of his is Mobile Development mainly in Xamarin and most recently using Flutter and Dart. Through this interest, he has released one app so far: Prog on both iOS and Android

3 thoughts on “Execute Script on Startup [Raspberry Pi]”

  1. I could never get this to work. But something similar did. Using your script to start Synergy, instead of /etc/xdg/lxsession/LXDE/autostart, I had to add a line to ~/.config/lxsession/LXDE-pi/autostart to open lxterminal on boot and run the command startsynergy – just as I would do to run synergy manually. The new line “@lxterminal –command startsynergy” in that config file did the job. One difference in these two files is that ~/.config/lxsession/LXDE-pi/autostart belongs to the user (no sudo prefix is required to edit it). I don’t know whether this has anything to do with my experience or not. I’d appreciate your thoughts. Thanks.

    1. Hi thanks for the comment 🙂

      No that sounds sensible for the graphical environment good job! One thing I should mention is that my version starts as soon as a terminal is open (hence why you starting the terminal on boot is kicking off the startsynergy script).

      Your input helps out the GUI users. If you don’t mind I’ll add it into the post to make it clear for readers? I’ll have to do this when I’m next at the laptop though as I’m on my phone right now.


  2. When I was reading other stuff about running scripts at startup, the discussion often included that the script ran when a terminal starts and would run again whenever the terminal was started again, or in certain cases it might run until it was stopped and etc. In trying to get the Synergy script to run, I never observed it running as soon as I started a terminal but before I had entered the startsynergy command. But then I wasn’t looking for that to happen. I should try it again. Naturally you can include my experience wherever you like. Your advice has been valuable as I’m not a Linux type. Yet. I’m, at long last, looking past MS though.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.