NateGreco NateGreco - 20 days ago 10
Linux Question

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?

Answer

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.