klricks
Posts: 8015
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Tue Dec 08, 2020 9:14 pm

How to use Autostart - Raspberry Pi OS (Desktop)

Note the settings and defaults described below are found in the Raspbian 'Buster' and later RPi OS releases.
Raspbian 'Stretch' and before releases used different defaults as noted below. ***

The autostart feature can be used to automatically start just about any app, script or command at boot (User login). Requires boot to Desktop (GUI) enabled.

Autostart is particularly useful when you need to run Desktop apps or any scripts which require Desktop (GUI).

Other methods such as rc.local or cron @reboot do not easily handle GUI programs as they attempt to start the app before the Desktop in ready resulting in failure to open.

There are 4 autostart methods or options:
• System (All Users) *** Default in 'Buster' and later OS releases ***
• User (Specific Users) *** Default in 'Stretch' and earlier OS releases ***
• Traditional (All Users) with .desktop files
• Traditional (Specific User) with .desktop files

System Method:
The System method is easiest as the required autostart file is already present and commands can easily be added with a simple edit. By default, the System autostart is applied to all users and executes each time a user logs in.
The System autostart file is located here: /etc/xdg/lxsession/LXDE-pi/

To open System autostart using the nano editor:

Code: Select all

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
See Using the Autostart File & Sample Autostart Files below.


User Method:
The user method works much the same as the System method but allows for a unique or custom autostart file for each user. If there is only one user (pi) or your multiple users do not require different apps or scripts started at login / boot, then there is no advantage to doing the user method.

The user autostart file and associated path does not exist by default.

The pi user autostart needs to be located here: /home/pi/.config/lxsession/LXDE-pi/ (If not user pi then substitute your username for pi /home/{user}/.config/lxsession/LXDE-pi/).

You will first need to create the lxsession and LXDE-pi sub directories then copy the System autostart to the user(s) location(s).

For user pi:

Code: Select all

mkdir /home/pi/.config/lxsession
mkdir /home/pi/.config/lxsession/LXDE-pi
cp /etc/xdg/lxsession/LXDE-pi/autostart /home/pi/.config/lxsession/LXDE-pi/
Note: If a user autostart file exists at /home/pi/.config/lxsession/LXDE-pi, then the System autostart file is totally ignored (for that user).

To open the User autostart using the nano editor:

Code: Select all

nano /home/pi/.config/lxsession/LXDE-pi/autostart
See Using the Autostart File & Sample Autostart Files below.

Using the Autostart File (System or User):

The default System autostart file:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
• The first 2 lines are important and must not be removed and must also be present if doing the User method. If you have made an autostart file without these lines, the desktop will boot to a blank (Openbox) screen. To recover, right click anywhere on the screen and select terminal from the menu. Then edit autostart and add those 2 lines.

• The xscreensaver command is only relevant if you have installed xscreensaver. If not then it can be removed or just ignored.

• The @ is optional. With @ present, if there is an error on first try, the system will attempt to run the command up to 4 more times before giving up.

• Note that the autostart file is not a bash script and is processed differently. The commands in the autostart file are processed independently at near the same time in a parallel fashion. Or the system does not wait for the previous command to complete before starting the next command. If you have multiple commands that are dependent on being run in a specific order then you would need to put your commands in a bash script, then call your bash script from autostart.

• All programs launched by autostart run in the background so no need to use & at the end of the command.

• It is important to use the complete paths to scripts in autostart. Also calls within a script need to have the complete path specified. Paths to installed apps are not required.

• Scripts must be made executable.

• If your script requires keyboard and/or console interaction, then use the lxterminal -e command. See examples and notes below.

• If your command or app requires network and does not seem to work from autostart, then try enabling the wait for network at boot option in sudo raspi-config

• In some rare cases you may need to add a delay before starting your script to give the Desktop more time to complete the booting process before your app starts. This can be done with a bash sleep command. Do not attempt to add the sleep command directly in autostart, it won’t work. Use the sleep command in a bash script instead.

Sample Autostart Files:

Example to start Calculator:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@galculator
Example to start browser:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@chromium-browser www.raspberrypi.org
Example to start a Python 3 script:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@python3 /path/my_script.py
Example to start a Python 3 script with terminal:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e python3 /path/my_script.py
Example to start a Bash script:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@bash /path/my_script
Example to start a Bash script with terminal:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal -e bash /path/my_script
Example to open Lxterminal without running a command:

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@lxterminal
Notes on using lxterminal -e:
(Notes below apply when lxterminal -e is used directly in autostart file. The noted behavior may different than what happens when run from the terminal command line).

No ‘ or “ in the path/filename to a script. This means that paths or filenames with spaces cannot be used. Use bash script instead.

Only 1 command per line. (Cannot use ; to specify multiple commands). Put multiple commands in bash script instead.

If the script or command exits to the command line for any reason then the terminal is immediately closed. This means if there is an error or the program terminates then you will not be able to see what happened as the terminal window will flash by too quickly.


Traditional System Method (All Users):
Beginning with the Dec 2020 release, Raspberry Pi OS now uses the /etc/xdg/autostart directory to start some background apps for printer etc. You can use this directory to start apps or scripts which will apply to all users.
Note that autostart here is a directory and not a file.
This method does not use an autostart file. It uses filename.desktop files instead. See example .desktop file below.

Traditional User Method (Specific User):
The traditional method also has a user based option. It requires your filename.desktop file(s) to be located here for pi user /home/pi/.config/autostart/ or for other user /home/{user}/.config/autostart/
You may need to create the auotstart directory if not present

Note that the System autostart file OR User autostart file if present, is run and processed along with the .desktop file(s).

In addition the .desktop files for system /etc/xdg/autostart and the .desktop files in the users home directory /home/pi/.config/autostart/ will all be processed.

Example .desktop file to start File Manager:

Code: Select all

[Desktop Entry]
Name=File Manager
Exec=pcmanfm
Type=Application
Give the file a unique name such as pcm.desktop and place it in /etc/xdg/autostart for system wide all users or /home/pi/.config/autostart for specific user.
You can have multiple .desktop files.
Last edited by klricks on Wed Jun 09, 2021 2:11 am, edited 1 time in total.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

User avatar
neilgl
Posts: 3569
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: How to use Autostart - Raspberry Pi OS (Desktop)

Thu Jan 14, 2021 8:04 pm

Good post thanks

Pearl.852
Posts: 124
Joined: Wed Jun 08, 2016 1:22 pm
Location: HKG (UTC+8:00)

Re: How to use Autostart - Raspberry Pi OS (Desktop)

Tue Mar 02, 2021 5:26 pm

Excellent information!! Thanks for posting.
Raspberry Pi Model B Rev 1 (256MB) ......... Raspberry Pi 2 Model B Rev 1.1
Raspberry Pi 3 Model B Rev 1.2 .................. Raspberry Pi 3 Plus Model B Rev 1.3
Raspberry Pi 4 Model B Rev 1.1 (4GB)

GlowInTheDark
Posts: 2035
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to use Autostart - Raspberry Pi OS (Desktop)

Tue Mar 02, 2021 5:46 pm

What a mess. Way too complicated - and seems to change every few releases of the OS.

All best avoided.

There are much better ways to startup processes on boot.
Poster of inconvenient truths.

Linux zealot and proud of it.

"nokcid" wants to date me.

User avatar
craigevil
Posts: 263
Joined: Wed Jan 27, 2021 5:22 am
Location: OZ
Contact: Website

Re: How to use Autostart - Raspberry Pi OS (Desktop)

Tue Mar 02, 2021 6:02 pm

With /home/pi/.config/autostart all you have to do for a User app is drag and drop.
Raspberry PI 400 Raspberry Pi OS (Debian Bookworm/sid) Kernel: 5.10.63-v8+ aarch64 DE: MATE Ram 4GB
Debian - "If you can't apt install something, it isn't useful or doesn't exist"

dexington
Posts: 1
Joined: Fri May 28, 2021 7:14 am

Re: STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Fri May 28, 2021 7:20 am

I'd like to know where the documentation is for this?

This is still buggy. I tried to start VNC Server using /etc/xdg/autostart/vncserver.desktop. This kept spawning new vnc server processes until the pi crashes.

I'd love to know why rc.local had to be disabled to implement this. Seems shortsighted, dumb.

klricks
Posts: 8015
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Fri May 28, 2021 2:45 pm

dexington wrote:
Fri May 28, 2021 7:20 am
I'd like to know where the documentation is for this?

This is still buggy. I tried to start VNC Server using /etc/xdg/autostart/vncserver.desktop. This kept spawning new vnc server processes until the pi crashes.

I'd love to know why rc.local had to be disabled to implement this. Seems shortsighted, dumb.
Autostart is run each time a user logs into the desktop (Not necessarily at boot).
Since starting vncviewer initiates a user login, autostart is run again and again and again........ until memory fills up with multiple instances of vncserver.
If you use a script to first check if vncserver is already running then you can use autastart.

Code: Select all

#!/bin/bash
if ps aux | grep '[/]usr/bin/vncserver'
then
     echo Already Running
else
     # echo Not running
     vncserver -geometry 1024x768
fi
This will assure that vncserver is only started once.
Note: Do not attempt to write the above script directly in the autostart file. Create a script file and then call the script file from autostart.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

GlowInTheDark
Posts: 2035
Joined: Sat Nov 09, 2019 12:14 pm

Re: STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Fri May 28, 2021 3:38 pm

dexington wrote:
Fri May 28, 2021 7:20 am
I'd like to know where the documentation is for this?

This is still buggy. I tried to start VNC Server using /etc/xdg/autostart/vncserver.desktop. This kept spawning new vnc server processes until the pi crashes.

I'd love to know why rc.local had to be disabled to implement this. Seems shortsighted, dumb.
rc.local has not been disabled. Where did you get that idea?

But, yes, you are absolutely right that "autostart" is a mess. But many posters on this forum seem to take a "It is what it is, so we have to use it as it is" attitude to it. I think it should just be either:

1) Fixed, so it works reliably and well. I have posted elsewhere about what I mean by this; I need not go into it here.

or

2) Removed.
Poster of inconvenient truths.

Linux zealot and proud of it.

"nokcid" wants to date me.

RossDv8
Posts: 260
Joined: Thu Jan 21, 2016 12:35 pm

Re: STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Tue Jun 22, 2021 8:00 am

I had a cow of a time getting Autostart working for my Pi4B. I could not get the RealVNC server that comes with the Pi (Enabled in raspi-config under 'Interfaces').

In the end I installed tigervnc-scraping-server (which is in the repos)

sudo apt install tigervnc-scraping-server

and that worked. Oddly, the most of the Remore Viewers in Linux Mint (my other computers) could not see it. The problem was solved when I installed the GNOME VNC client called vinagre

sudo apt install vinagre

Later I discovered RealVNC have a download page that has RealVNC Viewer for Linux, which doesn;t need to be installed. It is an executable file like an Appimage.

BACK to the Server.

To get a VNC Server booting at startup I had to use the Autostart method used by Ubuntu.

I created an autostart folder in /pi/.config

mkdir /pi/.config/autostart

Then I created a file in the autostart folder called StartVNC.desktop

The contents of the file are:


[Desktop Entry]
Type=Application
Name=StartVNC
Exec=x0vncserver -SecurityTypes=none
Terminal=true
StartupNotify=false


I made it executable, but I don;t know if that's needed since it is afterall a .desktop file
Terminal=true was supposed to run it in a Terminal so I could see if it was running, but that didn't work.

I also installed gufw

sudo apt install gufw

so that I could easily see if the ufw Firewall was running and see any Firewall Rules.

Then I made sure the port TigerVNC uses was open on all the computers by doing:

sudo ufw allow 5900/tcp
sudo ufw allow 5900/udp

In a Terminal on each including the Pi.

From there, just a reboot of the Pi, and when I started the VNC Viewers on the Linux computers, the Pi was sitting there happily in the window.

I have no idea why the built in RealVNC Server was not visible, but I am happy that the tigervnc-scraping-server was in the Repository :-)
Remember, nobody is listening to you
until you fart ...

electronicstoday
Posts: 48
Joined: Fri Nov 15, 2019 4:02 am

Re: How to use Autostart - Raspberry Pi OS (Desktop)

Mon Jul 12, 2021 6:57 pm

GlowInTheDark wrote:
Tue Mar 02, 2021 5:46 pm
What a mess. Way too complicated - and seems to change every few releases of the OS.

All best avoided.

There are much better ways to startup processes on boot.
Exactly. This is "The Linux way":

"We have 77 ways of doing that, you should try them all and see which works, but any arbitrary number of them are subject to change, largely undocumented, and without notice"

I don't have time to waste on this. Fail.

ViennaMike
Posts: 7
Joined: Sat Mar 04, 2017 11:34 pm

Re: STICKY: How to use Autostart - Raspberry Pi OS (Desktop)

Thu Oct 14, 2021 2:04 am

I have a related, but somewhat different issue. For a script I have, the script requires pulseaudio. I know this starts with the desktop, which explains why using autostart works, but cron and systemd are problematic. But what if I don't want to boot to desktop?

How can I have a script that requires that pulseaudio has been started run automatically on boot regardless of whether booting to CLI or the desktop, and if it makes a difference, regardless of using full Rasbian OS or the Lite version?

Return to “Raspberry Pi OS”