3

Let's assume we have a computer with Ubuntu Desktop installed on it. There are a few desktop environments (DE) installed such as: Unity, Gnome, KDE, XFCE, Mate, etc.

Also few users have their accounts and they regularly using the computer. Each user could change its DE according to its free will :)

How to determinate which is the current user's desktop environment, for certain user, through CLI within limited environment?

This question could be fragmented in this way:

  • In which file is stored this information?

  • What is the best way to read it through a script executed within an limited environment such as ssh session or cron?

  • How to read this information for random user?

  • I'm finding for one line command solution if it is possible.

1
  • The proposed dename.sh script doesn't recognize my Lubuntu 16.04 LXDE correctly, it says “KDE 4.14.16” instead probably because I have a bunch of KDE programs installed.
    – dessert
    Sep 17, 2017 at 13:34

2 Answers 2

3

Finally, I've managed to create script, that has 100% success with several dozen attempts within 13 different desktop environments.

The script analysis the content of each /proc/$PID/envion file of the subject user and determine which is its current desktop environment.

$ cat ~/find-DE.bash

#!/bin/bash

# Determinate the subject user - $USR
if [ "$1" == "simple" ]; then
                USR="$(id -u)"
                OUTPUT="simple"
elif [ -z "${1}" ]; then
                USR="$(id -u)"
                OUTPUT="$2"
else
                USR="$1"
                OUTPUT="$2"
fi

[ "$USR" == "$(id -u)" ] && SUDO="" || SUDO="sudo"

# Get the most frequent value of any array - https://stackoverflow.com/a/43440769/6543935
get_frequent(){
    awk 'BEGIN{FS=" "} {for(i=1;i<=NF;i++) print $i}' | \
    awk '
      {
          n=++hsh[$1]
          if(n>max_occ){
             max_occ=n
             what=$1
          }else if(n==max_occ){
             if(what>$1)
                 what=$1
          }
      }
      END { print what }
    '
}

# Get the numbers of all $USR's processes
PS=`pgrep -U "${USR}"`

# Get the values of $XDG_CURRENT_DESKTOP, $GDMSESSION, $DESKTOP_SESSION from each "/proc/$ProcessNumber/environ" file
for PN in $PS; do
        XDG_CURRENT_DESKTOP+=$($SUDO sed -zne 's/^XDG_CURRENT_DESKTOP=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
        GDMSESSION+=$($SUDO sed -zne 's/^GDMSESSION=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
        DESKTOP_SESSION+=$($SUDO sed -zne 's/^DESKTOP_SESSION=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
done

# Get the most frequent name of any desctop environment
# This is a way to find the current DE when it is changed a little bit ago
XDG_CURRENT_DESKTOP=$(echo -e ${XDG_CURRENT_DESKTOP[@]} | get_frequent)
GDMSESSION=$(echo -e ${GDMSESSION[@]} | get_frequent)
DESKTOP_SESSION=$(echo -e ${DESKTOP_SESSION[@]} | get_frequent)

# Print the output values
if [ "$OUTPUT" == "simple" ]; then
        echo "${XDG_CURRENT_DESKTOP[@],,}" | sed 's/\-.*//'
else
        echo "user: $(id -n -u $USR)"
        echo "uid:  $USR"
        echo "XDG_CURRENT_DESKTOP: ${XDG_CURRENT_DESKTOP[@]^}"
        echo "GDMSESSION:          ${GDMSESSION[@]^}"
        echo "DESKTOP_SESSION:     ${DESKTOP_SESSION[@]^}"
fi

Usage:

$ ~/find-DE.bash
user: spas
uid:  1000
XDG_CURRENT_DESKTOP: GNOME-Classic:GNOME
GDMSESSION:          Gnome-classic
DESKTOP_SESSION:     Gnome-classic

$ ~/find-DE.bash simple
gnome

$ ~/find-DE.bash 1001
user: guest
uid:  1001
XDG_CURRENT_DESKTOP: Unity
GDMSESSION:          Ubuntu
DESKTOP_SESSION:     Ubuntu

$ time ~/find-DE.bash 1001 simple
unity

real    0m1.587s
user    0m0.536s
sys     0m0.400s

More results:

#1
XDG_CURRENT_DESKTOP: Unity
GDMSESSION:          Ubuntu
DESKTOP_SESSION:     Ubuntu

#2
XDG_CURRENT_DESKTOP: GNOME
GDMSESSION:          Gnome
DESKTOP_SESSION:     Gnome

#3
XDG_CURRENT_DESKTOP: GNOME-Classic:GNOME
GDMSESSION:          Gnome-classic
DESKTOP_SESSION:     Gnome-classic

#4
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION:          LXDE
DESKTOP_SESSION:     LXDE

#5
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION:          Lubuntu
DESKTOP_SESSION:     Lubuntu

#6
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION:          Lubuntu-Netbook
DESKTOP_SESSION:     Lubuntu-Netbook

#7
XDG_CURRENT_DESKTOP: GNOME
GDMSESSION:          Openbox
DESKTOP_SESSION:     Openbox

#8
XDG_CURRENT_DESKTOP: KDE
GDMSESSION:          Plasma
DESKTOP_SESSION:     Plasma

#9
XDG_CURRENT_DESKTOP: XFCE
GDMSESSION:          Xfce
DESKTOP_SESSION:     Xfce

#10
XDG_CURRENT_DESKTOP: XFCE
GDMSESSION:          Xubuntu
DESKTOP_SESSION:     Xubuntu

#11
XDG_CURRENT_DESKTOP: X-Cinnamon
GDMSESSION:          Cinnamon
DESKTOP_SESSION:     Cinnamon

#12
XDG_CURRENT_DESKTOP: X-Cinnamon
GDMSESSION:          Cinnamon2d
DESKTOP_SESSION:     Cinnamon2d

#13
XDG_CURRENT_DESKTOP: MATE
GDMSESSION:          Mate
DESKTOP_SESSION:     Mate

:)

8
  • XDG_CURRENT_DESKTOP: LXDE | GDMSESSION: Lubuntu | DESKTOP_SESSION: Lubuntu – Congratulations! For the use in a script IMHO it should output just “lxde” though. Or maybe make it available via options?
    – dessert
    Sep 18, 2017 at 5:10
  • I just found unix.stackexchange.com/a/394763/246819 – maybe loginctl can be of use here?
    – dessert
    Sep 27, 2017 at 20:39
  • Hi, @dessert, thank you for this suggestion. Next week I will read about the command loginctl. I'm still working on my idea, but unfortunately few days ago I accidentally deleted the script, right then when I finished it :-) So I ran a GitHub project to prevent future silly mistakes as this. Now the new script is finished, it become lighter than the previous version, but the documentation and the tests are still in progress. Here is a demo of the previous version:
    – pa4080
    Sep 29, 2017 at 23:07
  • THE DEMO
    – pa4080
    Sep 29, 2017 at 23:08
  • 1
    Hi, @tresf, I haven't used Fedora, but in Ubuntu the window manager lightdm/gdm/etc. is owner of the desktop session while there is not any logged-in user. Whatever I think this project of mine could be interesting for you: github.com/pa4080/cron-gui-launcher
    – pa4080
    Feb 6, 2019 at 13:23
1
echo $DESKTOP_SESSION

shows the currently used desktop environment.

Find other good approaches here:

0

Not the answer you're looking for? Browse other questions tagged .