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

If you program is python that needs to run inside its virtual environment

#Instead of this:
COMMAND="python ./home/myfolder/my_application.py"

#Use this:
COMMAND="/home/myfolder/venv/bin/python3 /home/myfolder/venv/app/my_application.py"

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

Comments

  1. Some Guy Somewhere

    9 years ago

    How to start the application maximized, though?

    1. carlos cece

      5 years ago

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

    2. Some Guy Somewhere

      5 years ago

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

  2. Darryl

    10 years ago

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

    1. Arnold Chan

      5 years ago

      thank you

  3. stephan schulz

    11 years ago

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

    1. Arnold Chan

      5 years ago

      thanks