How to know which processes have the DISPLAY variable set?

I came up to this command after many searches and tries:

for file in /proc/[0-9]*; do grep -ao 'DISPLAY=[^[:cntrl:]]*' $file/environ 2>/dev/null && grep -ao '(.*)' $file/stat; done | sed 'N;s/\n/\t/'

A sample of the output is:

DISPLAY=:0  (unity-files-dae)
DISPLAY=:0  (unity-music-dae)
DISPLAY=:0  (unity-lens-vide)
DISPLAY=:0  (zeitgeist-daemo)
DISPLAY=:0  (zeitgeist-fts)
DISPLAY=:0  (zeitgeist-datah)
DISPLAY=:0  (cat)
DISPLAY=:0  (unity-scope-vid)
DISPLAY=:0  (unity-musicstor)
DISPLAY=:0  (dconf-service)
DISPLAY=:0  (gdu-notificatio)
DISPLAY=:0  (telepathy-indic)
DISPLAY=:0  (mission-control)
DISPLAY=:0  (goa-daemon)
DISPLAY=:0  (VBoxXPCOMIPCD)

To explain what's going on, this loop searches recursively in the /proc directory searching for DISPLAY in each file. Those files are really the processes running, so every file containing the word DISPLAY means that this process is using it.


With a small modification to the Maythux script, we can also get the PID of the processes using the DISPLAY variable.

for file in /proc/[0-9]*; do grep -ao 'DISPLAY=[^[:cntrl:]]*' $file/environ 2>/dev/null && grep -ao '[0-9]* (.*)' $file/stat; done | sed 'N;s/\n/\t/' |column -t |sort -n -k2

The output is:

DISPLAY=:0  590    (lxsession)
DISPLAY=:0  645    (unclutter)
DISPLAY=:0  705    (gvfsd)
DISPLAY=:0  710    (gvfsd-fuse)
DISPLAY=:0  727    (openbox)
DISPLAY=:0  729    (lxpolkit)
DISPLAY=:0  732    (lxpanel)
DISPLAY=:0  734    (pcmanfm)
DISPLAY=:0  772    (menu-cached)
DISPLAY=:0  781    (gvfs-udisks2-vo)
DISPLAY=:0  791    (gvfs-gphoto2-vo)
DISPLAY=:0  795    (gvfs-mtp-volume)
DISPLAY=:0  799    (gvfs-afc-volume)
DISPLAY=:0  804    (gvfs-goa-volume)
DISPLAY=:0  816    (gvfsd-trash)
DISPLAY=:0  21053  (npm)
DISPLAY=:0  21102  (sh)
DISPLAY=:0  21103  (sh)
DISPLAY=:0  21104  (node)
DISPLAY=:0  21110  (electron)
DISPLAY=:0  21112  (electron)
DISPLAY=:0  21149  (electron)
DISPLAY=:0  21154  (electron)
DISPLAY=:0  21180  (rec)

Ideally, all the current processes that correspond to the programs that you execute on the local machine, post login, are gonna carry the same display variable as your first open pts (pseudo-terminal session) console.

For instance, when you open your first terminal session (gnome-terminal) and run the who or w command you will notice some output like this:

$ who

yourusername   :0        2015-06-08 14:05 (:0)
yourusername   pts/0     2015-06-08 14:22 (:0)

or some folks might look like this (but not it your case)

yourusername   :0        2015-06-08 14:05 (:0)
yourusername   pts/0     2015-06-08 14:22 (:0.0)

using the light display manager if the DISPLAY variable is set differently for post login execution of shells (e.g. :0.0), then the environment variable for DISPLAY would yield the same display variable as the post-login display variable of the first open pts (:0.0), but the host variable, at login, would still be (:0).

By echoing the DISPLAY variable, or running set piped to less as shown below:

:~$ echo $DISPLAY

or

:~$ set | less

you can also check your sessions current display variable, and see what it is set to, for post login execution of shells. So basically the display variable of the processes you execute are gonna have the same display variable as your first open pts, post login.

The output of the script

for file in /proc/[0-9]*; do grep -ao 'DISPLAY=[^[:cntrl:]]*' $file/environ 2>/dev/null && grep -ao '(.*)' $file/stat; done | sed 'N;s/\n/\t/'

that Maythux posted, suggests that your first open pts is gonna have the same display variable as your display manager's login, in this case :0.

Now for the second example, the output would look something like this:

DISPLAY=:0.0    (gvfs-udisks2-vo)
DISPLAY=:0.0    (zeitgeist-daemo)
DISPLAY=:0.0    (zeitgeist-fts)
DISPLAY=:0.0    (zeitgeist-datah)
DISPLAY=:0.0    (gvfs-mtp-volume)
DISPLAY=:0.0    (gvfs-gphoto2-vo)
DISPLAY=:0.0    (gvfs-afc-volume)
DISPLAY=:0.0    (geyes_applet2)
DISPLAY=:0.0    (indicator-apple)
DISPLAY=:0.0    (cat)
DISPLAY=:0.0    (python)
DISPLAY=:0.0    (gvfsd-trash)
DISPLAY=:0.0    (indicator-keybo)
DISPLAY=:0.0    (gvfsd-burn)
DISPLAY=:0.0    (cat) 
DISPLAY=:0.0    (cat)
DISPLAY=:0.0    (gnome-terminal)
DISPLAY=:0.0    (bash)
DISPLAY=:0.0    (bash)
DISPLAY=:0.0    (sed)

Also for additional reading you can look at the the man pages for ptmx

$ man ptmx

This might lend some insight into the master-slave relationship of pseudo-terminals.