How do I determine which is the current user's DE through CLI within SSH or Cron? [duplicate]

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.


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

:)


echo $DESKTOP_SESSION

shows the currently used desktop environment.

Find other good approaches here:

  • How can I find which desktop enviroment I am using?
  • How to detect the desktop environment in a bash script?
  • How to return the currently active user/session on a Linux desktop?