In the article on ten things to do after after buying a Raspberry PI, item number 9 shows how to automatically start an application at boot time when booting to the command prompt. This blog article will now show how to auto start an application when booting into the desktop of the Raspbian operating system running on the PI. It also shows how to auto run a command line application or shell script in a terminal window on the desktop.
NOTE: This is one way to auto start applications, but not the only way. There are other methods to do the same thing.
Auto Starting a Raspberry PI Desktop Application
Some folders (directories) and a file need to be created as described here. How to do this is explained below the description.
- First create a folder called lxsession in the hidden folder called .config in the home directory.
- Now create a folder called LXDE-pi (name the folder LXDE in older Raspbian versions) in the lxsession folder.
- Finally create a text file called autostart in the lxsession folder.
You should end up with:
Or on older Raspbian versions:
Starting the Application Automatically
To start the application automatically, edit the autostart text file that you created above and add the application name that you want to start preceded by the @ symbol. For example, to auto start the calculator application (called galculator) the contents of your autostart file would look like this:
The next time that you start the desktop, the calculator application will automatically start.
If you write your own application that does not appear in the Linux PATH, then you will need to specify the path to the application, e.g. for an application in the home directory:
How to Do the Setup from the Desktop
To do the above steps on the desktop, start the file manager and then press Ctrl + H to show the hidden files and folders. Double-click the .config folder to open it. Right-click in the folder and use the pop-up menu to create the lxsession folder. Open the lxsession folder, right-click again and create the LXDE-pi folder. Open the LXDE-pi folder, right-click again and create the autostart text file. Double-clicking the autostart file will open it in the text editor. Add your application name to the file and save — don’t forget the preceding @ symbol as described above. Press Ctrl + H again any time that you want to hide the hidden files and folders in the file manager (Hidden files and folders have a period or dot (.) in front of their names in Linux. Ctrl + H toggles between hiding and showing these files in the file manager).
How to Do the Setup from the Command Line
On the command line in the pi home directory, enter:
mkdir -p .config/lxsession/LXDE-pi
Now enter the application that you want to automatically start, then save the file and exit (don’t forget the preceding @ symbol), e.g.:
Save the file in nano:
Ctrl + o
Ctrl + x
Auto Starting a Raspberry PI Command Line Application on the Desktop
To start a command line application in a terminal window on the desktop, you will need to do the above steps to auto start the terminal window called lxterminal. In the auto start file, add the following line:
To run a command line application in the terminal, it is necessary to use the same method that was used in the article ten things to do after after buying a Raspberry PI, item number 9. And that is…
Open the hidden file .bashrc found in the home directory and add the name of the application or script that you want to run. For example, to run the top command line application, the end of your .bashrc file will look like this:
# existing text in .bashrc file ...
This will run top in a terminal window the next time that you start the desktop.
To run a custom script in the terminal window on the desktop, insert the path to the script and the script name. E.g. for a script called myscript.sh found in the home directory:
# existing text in .bashrc file ...