Execute Script on Startup [Raspberry Pi]

Introduction

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.

Synergy Setup For Raspberry Pi

Introduction

Synergy is a cross platform tool that allows you to share your keyboard and mouse across any device with a synergy client running – this means you can use one keyboard and mouse and still use all your Mac/Windows/Linux devices (including your Raspberry Pi!).
I won’t talk about setting up Synergy on your PC, as this is extensively documented. If you want to know how to do this, here is a YouTube video showing how to setup Synergy for your main PC.
Instead I will focus on showing you how to install Synergy on your Raspberry Pi, more specifically your Pi running Raspian. Note that this tutorial will not benefit users of a headless OS such as Raspbian Lite.

Prerequisites

Before you begin, you must set up a synergy server on your main PC (the one with the keyboard and mouse you intend to use).

Whilst setting up the server, be sure to add a computer via the “Screens and Links” tab and name it “pi”. Your server setup should look something similar to the following:

Computer Setup for Synergy Server Image
Computer Setup for Synergy Server

Installing Synergy

To install Synergy on your Pi, we will be using the trusty apt-get package manager that we’ve used a number of times so far. The command is simply:
> sudo apt-get install synergy
If it asks you to continue simply enter “y” and hit enter.
That’s it! You have Synergy installed (aren’t package managers amazing?!)… but not yet configured!

Configuring Synergy Client

The last piece of the puzzle is to configure the pi as a Synergy Client, by telling it the name of the server you wish to connect to.
To do this, we will create a shell script which starts up synergy on our Pi using the connection settings we specify. To create this script, enter the following into the terminal:
> sudo touch /usr/bin/startsynergy

This will create our script named “start synergy” in the “/usr/bin” directory, which is on the system path by default, allowing us to execute the script anywhere with the command “startsynergy”.

Open up the script using the Nano Text Editor:
> sudo nano /usr/bin/startsynergy
Enter the following:
#!/bin/bash

killall synergyc    # Kill all previous synergy clients
sleep 1                 # Wait 1 second

synergyc --name pi <server name goes here>  # See below
exit 0   # Exit gracefully
Save the file by pressing “CTRL + X” and hitting “Y” to confirm the save.
The main contents on the script is the last line starting with “synergyc –name…
We are setting up a synergy client with the name pi (or whatever you specified as the pis name when you added a second computer on your synergy server).
Lastly <server name goes here> should be replaced with the name of your Synergy server, again, this is specified on your Synergy server setup.

Running Synergy Client

To run the script, we first need to tell the pi that this file should be executable. To do this enter the following:
> sudo chmod +x /usr/bin/startsynergy
Now finally, we can simply run the client with the following command in the terminal on the Pi:
> startsynergy
If your Synergy server was running, you should now be able to see “client pi connected” in the output log (on the server) and you should be able to use your keyboard and mouse seamlessly across your PC and your Pi.
TIP: To make your synergy script execute on startup, read my post on how to execute a script on startup.