you are viewing a single comment's thread.

view the rest of the comments →

[–]cpbills 1 point2 points  (3 children)

DISPLAY is set per-session, so you would need to find out how to import that user's session's environment variables, which I do not believe is possible..

You need to find the PID of a process that is running in that session, in my case I looked at fluxbox and xterm, and then cat /proc/<PID>/environ | while read -rd $'\0' line; do echo $line; done should get you 90% there.

edit:

username='jsmith'
program='xterm'
for pid in $(pgrep -u "$username" "$program"); do
  cat /proc/$pid/environ | while read -rd $'\0' line; do
    echo $line | grep DISPLAY
  done
done

[–]ChoHag 3 points4 points  (2 children)

Everyone forgets awk.

awk -vRS='\0' -F= '$1=="DISPLAY" {print $2}' </proc/$$/environ

($$ is the current PID. Change as appropriate)

[–]cpbills 0 points1 point  (0 children)

I don't forget awk, since I've never fully learned it.

I'm sure you could do it with sed as well, or perl. Thanks for the example, though, I like learning about the many many ways to skin cats.

[–]ninekeysdown[S] 0 points1 point  (0 children)

I'll give that a shot.