I'm implementing USB plug/unplug notification (here's related question), and I need to execute something like notify-send "device plugged" "My Device Title"
. The problem is that to make this command work, I should firstly set DISPLAY
, like this:
export DISPLAY=":0.0"
And secondly, this command should be called by appropriate user. Say, for user dimon
:
su dimon -c "notify-send 'device plugged' 'My Device Title'"
So, I need to get the list of all active X sessions and appropriate users, and call notify-send
for each user on his DISPLAY
.
I tried to use w
for that, example output at Linux Mint 13 MATE:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
dimon tty8 :0 Sun15 3days 1:38m 1.95s x-session-manager
dimon pts/0 :0 Sun15 0.00s 0.20s 0.00s tmux
So we have both username and display. I decided to parse it like that:
declare -a logged_users=(`w |grep -vP "^(USER| )" |awk '{if (NF==8){print $1" "$3} else {print $1" :0"}}' |sort |uniq`)
Now, I have array logged_users
: [0]
contains dimon
, and [1]
contains :0
. This would be great, but unfortunately it works not everywhere. Say, on Ubuntu 12.04 with lightdm we have this w
output:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
nui tty7 18:22 35:56 1.66s 0.11s gnome-session -
nui pts/0 :0.0 18:55 5.00s 0.20s 0.00s w
No idea why there's no FROM value for gnome-session
.
And even worse one, at xubuntu:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
nui tty7 15:50 31:07 52.55s 0.13s /bin/sh /etc/xd
No display at all! If other user is logged in, no display is specified for him too (but actually it's :1.0
)
So, I'm looking for another approach. I also know we have a list of all active X sessions here: /tmp/.X11-unix
, and I can get the list of them like this:
cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done
But then, I don't know how to retrieve users.
So, how to get the list of all active X sessions and appropriate users?
pgrep Xorg
instead. I have 2 running now so how would this handle multiples? – slm Feb 26 '14 at 19:08pidof -s /usr/bin/Xorg
returns nothing. And, actually, I'm looking for the way that will work on (almost) all *nix systems, but people complains above that it doesn't work for them, so, it's not an option for me, unfortunately. – Dmitry Frank Feb 27 '14 at 07:16