How To Autostart Apps In Rasbian LXDE Desktop

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.

Method 1

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
@xscreensaver -no-splash@
@lxterminal
@leafpad

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.

Method 2

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 :

sudo nano ~/.config/lxsession/LXDE/autostart

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:

@lxterminal
@leafpad

To save and exit the nano editor press CTRL-X, Y and then ENTER.

Test

Once your configuration file has been updated you are ready to test.

startx

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.

Auto-run Minecraft

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 :

@~/mcpi/minecraft-pi
@lxterminal

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 :

@/usr/bin/python /home/pi/example.py

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.

LXDE Desktop

Note : 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.

This entry was posted in Raspbian, Tutorials & Help and tagged , , . Bookmark the permalink.



16 Responses to How To Autostart Apps In Rasbian LXDE Desktop

  1. Paul Adomshick says:

    Will the python script open in a terminal window or run as a process in the background? I’d like to be able to get a python script that outputs text (generated from sensor outputs) to automatically run in a terminal window. Is there an easy way to accomplish that?

    • Matt says:

      It runs in the background … but you can launch LXTerminal instead and pass it a command line parameter to run a Python script. Haven’t tested it but you could try something like :

      @lxterminal -e ‘/usr/bin/python /home/pi/myscript.py’ -t ‘MyTitle’

  2. Don Sorensen says:

    Looks like a “code” tag got misplaces in your first snippet above. I’m pretty sure the lines “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.” don’t belong in the autostart file. I’m sorry if it sounds picky, but I’ve been fighting the way Wordpad puts tags into posts for about a week now. Thought you’d like to know.
    Great site you’ve got here. Thank you for going through the trouble.

    • Matt says:

      Thanks Don. I spotted that just before I published it … then obviously forgot to actually correct it! Thanks for pointing it out. It’s sorted now.

  3. Jeremy says:

    I couldn’t figure out why I couldn’t get this to work.
    In the latest raspbian image I downloaded (around Dec 30, 2014) they changed the path for the autostart file that is used.
    This in method #1:
    sudo nano /etc/xdg/lxsession/LXDE/autostart
    is now this:
    sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
    I hope that saves someone some time.

  4. Daniel says:

    As Jeremy was saying, with the changes to the new image for Raspbian, it also effect the per user basis. I tried to figure it out for hours why my apps were not auto starter per user and you have to change the folder ~/.config/lxsession/LXDE to ~/.config/lxsession/LXDE-pi for it to work properly. Hopes this helps.

  5. Ben says:

    Thanks Jeremy – I am trying to use the user autostart (to start pipresents on startup) and it didn’t work until i created: ~/.config/lxsession/LXDE-pi/autostart

    Reading the above I guess this is new – I downloaded and installed NOOBS today, 17th Jan 2015. Hope this helps.

  6. Carlos says:

    Thanks all,

    I was having issues with this as well… I had set the pi to autostart into GUI (thru raspi-config) and all the instructions never made this work!!!

    However, figured out that if I disabled the autostart into GUI (via raspi-config) and run the command startx, it started working…

    any ideas how to make it boot the pi into GUI and have auto start of an application.

    My second issue is that I can now see my python script trying to execute (by running startx manually), but can’t start since input() has an EOF error…

    so I basically want to boot to GUI and ready for keyboard input.

    Something relatively easy, but has been taking me several days to figure because of all the oddities of Raspian/Pi

    Thanks,

    -Carlos

  7. David van Wyk says:

    Hi Guys,

    I am busy with an application that utilises the GPIO and it needs root priviliges, but I want it to auto start on boot.

    Do I need something extra, as on the command line ‘sudo’?

    I usually have to open the current folder as root and then I can start my application by double clicking.

    The above code doesn’t start my application up.

    Thanks.

    • Howard says:

      I used the following command to change the file ownership to pi and switch back to root afterward.

      chown pi:pi [filename]

  8. Henry Hansel says:

    I too have been battling this on a RPI2 running Rasbian. My app uses GPIO and tkinter so I need X running. My intention is to auto boot to desktop and then for my application to launch covering the OS like a virtual machine. Strange behavior is that after locating the path .config/lxsession/LXDE-pi/ and creating my autostart file my python program will auto launch about 50% of the time on boot. I have not been able to determine why it will not the other times? I hope that someone can finally put a nail in this, I am dead in the water with this project until I can get 100% autostart to work. Thanks for all the input.

    Henry

  9. Henry Hansel says:

    Finally, I think that I have got autostart to work 100% of the time.
    I had to modify my autostart file in

    /etc/xdg/lxsession/LXDE-pi/autostart

    to read:
    @/usr/bin/python3.2/myscript.py

    myscript.py resides in home/pi

    I added python3.2 for it to work, apparently it was trying to run my python3 script with python2 even though the shebang statement at the top of the script calls for python3. I am a linux noob so any insight on why this works would be appreciated.

    Henry

  10. Ashley Burt says:

    I just put the commands I needed to autorun into the users .profile in their home directory. Worked like a champ.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>