DrTarr
DrTarr

Reputation: 952

Autostart GUI application with LXDE session

There's quite a bit of information out there on this topic, but for some reason I just can't get it to work. This is on a raspberry pi running the 'DietPi' flavor over the raspian distro, and is perhaps what separates my question from the others.

So I have a GUI application that I wish to launch at boot, after the LXDE session has begun. So I have utilized the following file here:

/etc/xdg/lxsession/LXDE/autostart

and added the line:

@/myapplication

This works, however, it launches multiple instances of this program, and the first one always crashes. This creates problems because there's some competition for resources (IO, files, etc). So what I did was create script file, /myapplication-autostart.sh instead like so:

if pgrep "myapplication" > /dev/null
then
   echo "my application is already running"
else
   /myapplication
fi

and then changed /etc/xdg/lxsession/LXDE/autostart to @/myapplication-autostart.sh. Now what I find is the program launches once, but the instance crashes. It crashes when it attempts to create a window (opencv imshow). This is strange because the program will also run headless if an X-session is not available, but for some reason it crashes and I do not know where to check why.

Also, to test it wasn't an issue with the script file, I commented everything out except the /myapplication and I have found the script file runs in a continuous loop and every time I close the application it opens back up. I'm not sure why this is either.

I've tried adding a sleep delay in the script and it does not help. For whatever reason, it seems the LXDE autostart script is ran at least 3 times when booting the pi and the circumstances around the first cause the program to crash. Does anybody understand this sequence and behavior of calling this autostart script?

Upvotes: 2

Views: 7138

Answers (4)

PeteC
PeteC

Reputation: 129

This is an old thread but I was having problems getting autostart to start all the tasks listed. After many days I concluded there were one or more "invisible" characters that autostart didn't like. So I deleted the lines for the tasks that didn't start and retyped them. That solved the problem!

I think I corrupted the lines because I was editing some of the lines on my Windows computer. It was likely inserting CR with LF or some other stuff. I WILL NEVER EDIT TEXT FOR LINUX USING WINDOWS!

Maybe someone else will hit this problem and this may help them. I don't know where else to put this information.

Upvotes: 1

sebix
sebix

Reputation: 3230

It is also possible to use the XDG standard Autostart - which is independent of the used desktop environment - by placing desktop files at

  • $XDG_CONFIG_HOME/autostart (by default ~/.config/autostart)
  • or for system-wide autostarts at $XDG_CONFIG_DIRS/autostart (by default /etc/xdg/autostart).

Such a .desktop-file could look like:

[Desktop Entry]
Type=Application
Version=1.0
Name=JDownloader
Exec=/usr/local/bin/my-application.sh
Categories=Utilities

The specification of desktop-files can be found at freedesktop.org.

Upvotes: 4

DrTarr
DrTarr

Reputation: 952

Here was the final solution...

/etc/xdg/lxsession/LXDE/autostart added the line:

/myapplication-autostart.sh

and /myapplication-autostart.sh was changed to:

#!/bin/bash
if pgrep "myapplication" > /dev/null
then
   echo "my application is already running"
else
   if [[ "$DISPLAY" = ":0" ]]
   then
       /myapplication
   fi
fi

I had to write the display variable to file in combination with the errors to file to discover the issue. At login 2 X sessions were created, display ":1" and display ":0", in that sequence. Display ":1" crashed because, although not headless, it was not initialized to a particular resolution and there was some resizing code in my program. Display ":0" was the actual display on the HDMI out and the one I wanted. Really, the conditional check to see if the application isn't necessary but I left it in there to be safe. I could have also left the @ on the LXDE autostart file but it got annoying in the cases I wanted to close the application because it'd keep re-opening. Possibly I'll put it back later.

Thanks for the help!

Upvotes: 3

Jamil Said
Jamil Said

Reputation: 2083

First, some comments about opening several instances of the program: when you use "@" at the beginning of the line on the startup file (ex.: @/myapplication), this requests your system to try to launch the program, but if the program fails to open correctly, then the system will try to open it multiple times until it opens correctly -- if you remove "@" from the line beginning, then the system will only try to open the program once.

Now, to find out why the program is failing, I advise you to add

2> /file/log

to the end of every command on your script. Doing so would append any error message to a log (/file/log), and analyzing those error messages would be key to find out why the program is misbehaving.

One important note: if your program needs root privileges to run, then it will fail when called via /etc/xdg/lxsession/LXDE/autostart, as this method calls programs without elevated permissions.

Upvotes: 1

Related Questions