If you use the Raspbian operating system on your Raspberry Pi you will be aware that when you type startx you launch the graphical user interface “LXDE”. Within this environment there are plenty of applications and utilities. In your projects you may want to auto-load one or more of these applications when you run startx to save you having to launch them manually.
Popular choices might include LXTerminal, Scratch, Midori and Leafpad.
There are two methods you can choose. For most people either one will work but if in doubt use Method 1. In the examples below I’ve added two applications (LXTerminal and Leafpad) but you can add however many you need.
Start by booting your Pi to the command prompt.
This method uses a global list of autostart applications that applies to all user accounts. They will load whenever LXDE is loaded regardless of what user is logged in at the time. Usually this user is the default ‘Pi’ account.
Use the following command to launch the nano text editor and edit the autostart file :
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
Note : The newer Raspbian image changed the folder “LXDE” to “LXDE-pi” so the line above has been updated.
After the last line add a line for each application you wish to autorun. For example if I add lines for LXTerminal and Leafpad my file looks like this :
@lxpanel --profile LXDE @pcmanfm --desktop --profile LXDE @lxterminal @leafpad @xscreensaver -no-splash@
To save and exit the nano editor press CTRL-X, Y and then ENTER. You can use your preferred text editor if nano isn’t your first choice.
The alternative method is to create a configuration file that is unique to the currently logged in user. First you need to edit this text file :
As in Method 1 this file represents a list of commands to be executed when the GUI loads. It is usually blank when you first edit it so just add the applications you need to auto-load:
To save and exit the nano editor press CTRL-X, Y and then ENTER.
Once your configuration file has been updated you are ready to test.
The LXDE desktop should load and your chosen applications should launch.
Both Methods At The Same Time?
You can use both methods. The applications in each configuration file will launch as expected. This would allow you to launch specific applications for everyone while allowing individual users to have their own apps launch. How useful this is will depend on whether you have added other user accounts to your installation.
Assuming you have already installed Minecraft on your Pi you can auto-run it as well. When I am in a Python-Minecraft mood I launch Minecraft and a terminal window ready for executing scripts using the following lines :
This assume the Minecraft executable is located in ~/mcpi/ (same as /home/pi/mcpi/).
Auto-run Python Scripts
You can auto-launch your own Python scripts by adding the line :
This works best with Method 2 as in this example the Python script is stored in the home directory of the default Pi user. If another user is logged in they wouldn’t have access to this directory so LXDE may not be able to autoload it.
Whichever method you used just type startx from the command line and your chosen applications should load automatically.
Note 1 : The order that you place the application commands doesn’t always mean they will load in that order as different applications will take different amounts of time to load completely. I would tend to put the larger applications first so they have as much time as possible to fire up. i.e. Midori before LXTerminal.
Note 2 : Make sure your commands come before the @xscreensaver line.