Screensaver

If you need to disable the screen saver / screen auto switch off see here.

Auto-running a Python file within a terminal window

Edit the X Windows autostart file:

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

Add the following line to the end:

@lxterminal -e python /home/pi/myprogram.py

So you end up with something like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e python /home/pi/myprogram.py

That’s it, reboot and you should see a terminal window automatically open and run your program.

Auto-running a bash script within a terminal window

Edit the X Windows autostart file:

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

Add the following line to the end:

@lxterminal -e /home/pi/myscript.sh

So you end up with something like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e /home/pi/myscript.sh

Make sure your script is executable (right click > Properties > Permissions).
That’s it, reboot and you should see a terminal window automatically open and run your program.

Auto running a program with auto restart on crash

You can use a simple bash script that runs the program and re-runs it should it crash, with writing restart events to a log file:

#!/bin/sh

COMMAND="python ./home/myfolder/my_application.py"
LOGFILE="/home/myfolder/restartslog.txt"

writelog () {
  now=`date`
  echo "$now $*" >> $LOGFILE
}

writelog "Starting up"
while true ; do
  $COMMAND
  writelog "Exited with status $?"
  sleep 5      #Pause before restart (optional)
  writelog "Auto restarting app"
done

Make the .sh file executable and then run it. To forcibly exit use: CTRL+C

Auto run the bash script as detailed above on this page

Run with a delay before launching program

To run with a delay before your app launches, instead of adding the path to your app, provide a path to a script we will create:

@sh /home/pi/myapp_start.sh

Exit and save, then create the script file:

sudo nano /home/pi/myapp_start.sh

Insert the following into the file, setting the delay you want in seconds and the path to your app executable:

#!/usr/bin/sh
sleep 4
/home/pi/projects/myfolder/myapp

Exit and save, then finally make the script executable:

sudo chmod +x /home/pi/myapp_start.sh

That’s it, reboot and your app should launch after the delay period

Killing the application while running

Plug in a USB keyboard if necessary and press ALT+F4

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. stephan schulz

    8 years ago

    for me on raspberry pi2 autostart was located here
    sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

    1. Arnold Chan

      3 years ago

      thanks

  2. Darryl

    7 years ago

    Mine was at /etc/xdg/lxsession/LXDE/autostart

    1. Arnold Chan

      3 years ago

      thank you

  3. Some Guy Somewhere

    7 years ago

    How to start the application maximized, though?

    1. carlos cece

      3 years ago

      did you find out ?? i’m trying to launch thonny at te start up…

    2. Some Guy Somewhere

      3 years ago

      I did, but I don’t remember any more. It was not trivial, I had to change the command line I believe

Comments

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