Facebook Twitter Instagram Pinterest YouTube
    Trending
    • Pi Pico W Launched
    • Add Kodi to RetroPie Menu
    • Disable Auto-login in Raspberry Pi OS
    • Raspberry Pi Cloud Storage with MEGA
    • RetroPie Temperature Monitor from Menu
    • Pi Pico Pinout and Power Pins
    • Install Arduino IDE on Raspberry Pi
    • Raspberry Pi 400 SSD Upgrade
    Facebook Twitter Instagram Pinterest YouTube RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        May 6, 2022

        Add Kodi to RetroPie Menu

        February 26, 2022

        Disable Auto-login in Raspberry Pi OS

        February 2, 2022

        Raspberry Pi Cloud Storage with MEGA

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Software»Raspbian»How To Autostart Apps In Rasbian LXDE Desktop
      LXDE Desktop

      How To Autostart Apps In Rasbian LXDE Desktop

      33
      By Matt on May 3, 2014 Raspbian, Tutorials & Help

      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
      @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.

      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?

      Since v0.4.9 If both files are present, lxsession only executes the local file. This means you can no longer use both methods at the same time. Method 2 will take priority over Method 1.

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

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleHow To Use Gnuplot To Graph Data On The Raspberry Pi
      Next Article How To Capture Minecraft Screenshots On The Raspberry Pi

      Related Posts

      Add Kodi to RetroPie Menu

      Disable Auto-login in Raspberry Pi OS

      Raspberry Pi Cloud Storage with MEGA

      33 Comments

      1. Paul Adomshick on May 4, 2014 12:20 am

        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?

        Reply
        • Matt on May 4, 2014 10:16 am

          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’

          Reply
          • Paul Adomshick on May 6, 2014 4:13 am

            Much appreciated. I’ll try that.

            Reply
          • AJ on December 2, 2015 3:35 am

            IMPORTANT: this line has to be added before the @xscreensaver line. Finally worked for me after reading about that here: https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=65607

            Reply
            • Matt on December 3, 2015 7:23 pm

              Thanks, I’ve added a note at the bottom of the post.

              Reply
      2. Don Sorensen on May 4, 2014 8:16 pm

        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.

        Reply
        • Matt on May 4, 2014 8:45 pm

          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.

          Reply
      3. Jeremy on January 11, 2015 7:23 pm

        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.

        Reply
        • goodsayan on January 12, 2015 11:15 am

          folder LXDE has changed, Thanks Jeremy!

          Reply
        • Claudio on January 13, 2015 11:18 pm

          THANK YOU! This saved me really time!

          Reply
      4. Daniel on January 16, 2015 3:29 am

        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.

        Reply
      5. Ben on January 17, 2015 2:28 pm

        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.

        Reply
      6. Carlos on February 18, 2015 9:39 pm

        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

        Reply
      7. David van Wyk on February 24, 2015 3:07 pm

        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.

        Reply
        • Howard on April 14, 2015 10:09 am

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

          chown pi:pi [filename]

          Reply
          • David van Wyk on June 7, 2015 10:15 am

            Howard, I am not quite with you? I have since also upgraded to the Rpi 2B, but still having trouble starting my app.
            Now on the Rpi 2B I cant switch a window to root user, the option was removed. So my app doesn’t even start on normal double click anymore. It only runs if I remove gpio references from the code.

            How do I run a GUI with GPIO (root privileges) on Rpi 2B and more especially how can I autostart the app. I changed the file ownership to root, still doesnt work.

            Reply
      8. Henry Hansel on February 25, 2015 12:05 pm

        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

        Reply
      9. Henry Hansel on March 1, 2015 12:01 pm

        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

        Reply
        • cc on May 2, 2016 3:21 pm

          Hi,

          I have installed the raspbian using Noobs and Tried the Autostart steps given below.

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

          Added @lxterminal

          then rebooted the system my system boots automatically without login in Pi user but it does not open the terminal automatically.

          Then I open it manually and done the startx on terminal.
          It opens the terminal automatically .

          But next time when I putted a reboot command & reboot my system ,it asked me a login ID and password on GUI to login to Raspnerry pi .

          I tried to login using username pi and password raspberry ,but I am not able to login to the session.

          Please suggest the solution why i am not able to login to PI & also I want it to Login automatically in LXDE session without asking user and password.

          I have tried it by chnaging the pi password and login with the new password still it not worked.

          Also I have added a new test user and tried the same steps .
          nano /etc/xdg/lxsession/LXDE-pi/autostart

          Added @lxterminal

          now it able to login with my new username and password .But I want to login to this session automatically & open the terminal.

          I tried the rasp-config but not worked .

          Please suggest solution .

          Thanks.

          Reply
      10. Ashley Burt on March 17, 2015 8:03 pm

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

        Reply
      11. Rebecca on April 11, 2015 4:51 pm

        Is it possible to have multiple python scripts set to run at the start up of the GUI? I have tried just adding in another @/usr/bin/python /home/pi/example.py line to the autostart file but it doesn’t run the second python script

        Reply
        • Frank on October 6, 2015 2:35 pm

          I was having issues trying to get the R Pi2 to launch a website on boot I ended up making the change to this config file to make it work

          sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

          It worked like a charm. I had a hard time finding this solution and i hope it helps others.

          Reply
          • Sal on October 26, 2015 5:18 am

            Frank thank you spent hours and hours looking for this solution, editing

            sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

            Worked perfect!

            THANKS!!!!

            Reply
      12. Mark Baker on October 28, 2015 3:15 pm

        I’ve modified /home/pi/.config/lxsession/LXDE-pi/autostart adding @xfreerdp server1 to automatically start a RDP session to a terminal server. It works perfectly if I manually login as pi but as soon as I reconfigure it to automatically login as pi into the desktop it stops working. If I add @leafpad to the autostart file it works but not the @xfreerdp Any suggestions?

        Reply
      13. Klaus Werner on November 29, 2015 3:33 pm

        Hi, first thank you for the tutorial. it works perfect to start a terminal.
        Do this

        sudo sed /etc/lightdm/lightdm.conf -i -e “s/^autologin-user=.*/autologin-user=root/”

        and you will be root after login as pi to LXDE.

        After you can put all scripts you need in .bashrc from root.

        regards from rainy Germany

        Klaus Werner

        Reply
      14. Peter Triandafillou on January 24, 2016 10:42 pm

        This thread has been very helpful – thanks to all the contributors.

        I have a question: I added a line to start an lxterminal to run my program, and it works. However, my program accesses a Sense Hat, and it needs to start with sudo to run properly. Is there a way to specify the user password in the autostart line so that it doesn’t need to be entered? I’m developing an application that would be better off without a keyboard.

        Thanks!

        Reply
      15. Alex Cahill on February 11, 2016 10:59 pm

        Annonyingly I took me 2 days to figure out that method 1 no longer works.

        https://wiki.archlinux.org/index.php/LXDE#Autostart

        “Note: If both files are present, lxsession only executes the local file as of v0.4.9”

        Reply
        • Matt on February 15, 2016 8:27 pm

          Method should work as long as there are no local files defined. Thanks for the tip, I’ve modified the article to mention the local file take priority over the global file.

          Reply
      16. Peter on February 23, 2016 12:54 pm

        I have a small problem. I want to autostart two programs and of cause it works with mentioned method no.1. The problem is, I want to start them in two different virtuell desktops, unfortunately both programs are starting only on one desktop and I really do not find a way to start on the defined desktops. Any idea?

        Reply
      17. jordan on March 12, 2016 11:09 pm

        THANK YOU

        I have been trying to find a way to get this to work, and i’ve seen other tutorials like this, but only using method 1 and I could never get it to work. Method 2 worked perfectly for me.

        Thank you again!

        Reply
      18. ALI on April 10, 2016 6:39 pm

        Can I run a shell file placed on Desktop on reboot?? How to do that?

        Reply
      19. Kevin on August 29, 2016 8:00 pm

        The second method worked for me, but it boots my kivy app vertically as opposed to horizontally. Is anyone else getting this problem? Any help would be much appreciated.

        Reply
        • Kevin on August 29, 2016 8:04 pm

          I just found the solution. If this happens to you instead of typing:

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

          Type instead:

          @sudo python example.py

          like you would in the terminal.

          Reply

      Leave A Reply Cancel Reply

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

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      February 2, 2022

      Raspberry Pi Cloud Storage with MEGA

      January 7, 2022

      RetroPie Temperature Monitor from Menu

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      3D Printing Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter LCD LED Linux media Minecraft Model A Model B motionEyeOS PCB photography photos Pi-Lite portable power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives
      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      Facebook Twitter Instagram Pinterest YouTube RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2022 - All Rights Reserved - Matt Hawkins

      Type above and press Enter to search. Press Esc to cancel.