silly05
Posts: 11
Joined: Mon Nov 19, 2018 6:45 pm

differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Sun Jun 13, 2021 7:10 pm

Hello

I am familiar with two ways to autostart programs on boot.
for console programs I use a bash script at the end of
/home/pi/.bashrc

for programs that start after the Desktop loads (after changing the boot options) I use
a command in
/etc/xdg/lxsession/LXDE-pi/autostart


I am using a Raspberry pi 3B+ and 32-bit Raspberry pi OS

The result I am getting is that the program is started 2X if the desktop is loaded because the .bashrc runs AND the autostart as well. If I boot to the console it works nicely.

Is there a way I could set an environment variable for one boot type or the other - example in the LXDE configuration that I could then use in the .bashrc to prevent the script starting when I boot to the desktop?

I have tried capturing and printing the environment and unfortunately they are both identical at the early stages at least on my setup.

the boot type can be set with the Raspberry pi config gui and application of course so it would be great to have this correction be automatic

silly05
Posts: 11
Joined: Mon Nov 19, 2018 6:45 pm

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Sun Jun 13, 2021 8:20 pm

this is not a solution but I found that when the script is executed with lxde autostart

XDG_SESSION_TYPE=x11

and when it is started from the console it is this:

XDG_SESSION_TYPE=tty

However these are noticed after the fact and there is a race condition as both instances start at slightly different times, the console instance before the desktop one.

I am hoping for an explicit way to set an environment variable for the LXDE Desktop such that it can be used a test in the script.

User avatar
thagrol
Posts: 5218
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Sun Jun 13, 2021 9:14 pm

You should probably rethink your start method.

/etc/xdg/lxsession/LXDE-pi/autostart applies to all users not just to pi. If you ever add another user they'll start your program too.

/home/pi/.bashrc runs a lot more often than you apppear to think:
  • At login on the console (icluding auto login whether to command line or GUI)
  • At login via ssh, serial, telnet, etc.
  • Every time you open a new terminal window in the desktop
  • Every time you open a new tab in an already open terminal window.
  • ...
The best* approach is likely to be one of a systemd service, an @reboot cron job, /etc/rc.local, or a .desktop file in /home/pi/.config/autostart however a lot depends on what you are trying to launch and what services it needs.

Or stick with your current setup but have autostart and .bashrc check if the process is already running and only start it if it is not.

*: for a suitable value of "best"
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

silly05
Posts: 11
Joined: Mon Nov 19, 2018 6:45 pm

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Sun Jun 13, 2021 9:48 pm

thanks so much for the detailed response. It gives me a lot to work with. there probably isn't an elegant solution to the thing I was trying to do but there are any number of workarounds such as brute-force sensing of double-starting.

I'll study the ideas and see if I can set up a more elegant solution.

currently I manually switch some code with # back and forth when I activate boot to desktop or boot to console. The function is perfect but it requires editing those files and that is not a good long-term solution for me.

cheers!

swampdog
Posts: 719
Joined: Fri Dec 04, 2015 11:22 am

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Mon Jun 14, 2021 3:55 pm

https://www.thegeekdiary.com/what-is-th ... -in-linux/
^^^typically "/etc/bash.bashrc" rather than "/etc/profile".

eg: I put things like "export HISTTIMEFORMAT='%F %T ' HISTFILESIZE=2500" in /etc/bash.bashrc so that it applies to all user bash logins whereas I put "PATH=$PATH:/usr/local/sd/bin" in "/home/foo/.bash_profile" so that only the "foo" user gets to see that extra PATH.

It's usually enough to use the DISPLAY variable to distinguish: it won't be set for consoles..

Code: Select all

#!/bin/bash

fcp_main ()
{
 local  m

 touch /tmp/z && chmod 666 /tmp/z
 [ -z "$DISPLAY" ] && {
        m="I'm a console"
        echo `date`" ""$$"":""$PPID"":""$m" >> /tmp/z
        echo "$m"
        } || {
        m="I'm a GUI"
        echo `date`" ""$$"":""$PPID"":""$m" >> /tmp/z
        lxterminal -t "$m" &
 }
}

fcp_main

Code: Select all

foo@pi20:~ $ crontab -l
# m h  dom mon dow   command
@reboot /wrk/wibble
..or ~/.bash_profile so it only loads for a login shell. May also want to write it's "pid" somewhere if you don't want it to start twice ($$ is the pid above).

Code: Select all

foo@pi20:~ $ cat /etc/xdg/lxsession/LXDE-pi/autostart 
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@xclock --geometry 100+100
@/wrk/wibble
(reboots rpi..)

Code: Select all

foo@pi20:~ $ cat /tmp/z
Mon 14 Jun 16:52:50 BST 2021 474:473:I'm a console
Mon 14 Jun 16:52:54 BST 2021 1006:935:I'm a GUI

User avatar
jojopi
Posts: 3490
Joined: Tue Oct 11, 2011 8:38 pm

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Mon Jun 14, 2021 5:14 pm

swampdog wrote:
Mon Jun 14, 2021 3:55 pm
~/.bash_profile
Aside, you should not create ~/.bash_profile, because that will take precedence over Debian's existing ~/.profile, and that in turn means that ~/.bashrc will also not be read.

A lot of advice on the internet about startup files is accidentally distribution specific. You should always check which files already exist and what they do, first.

swampdog
Posts: 719
Joined: Fri Dec 04, 2015 11:22 am

Re: differentiate console boot from desktop boot in bash for autostart - using environment variable? or?

Mon Jun 14, 2021 5:57 pm

jojopi wrote:
Mon Jun 14, 2021 5:14 pm
swampdog wrote:
Mon Jun 14, 2021 3:55 pm
~/.bash_profile
Aside, you should not create ~/.bash_profile, because that will take precedence over Debian's existing ~/.profile, and that in turn means that ~/.bashrc will also not be read.

A lot of advice on the internet about startup files is accidentally distribution specific. You should always check which files already exist and what they do, first.
Good point. I forget I have things set up for multiple shells. I source ~/.bashrc in my ~/.bash_profile for that reason.

Return to “Advanced users”