Sample Header Ad - 728x90

how to know graphic current user(s) and DISPLAY(s) by root

0 votes
0 answers
101 views
Gentoo, openrc, X11. Hi, For my suspend process, I need to get something like
[user] [DISPLAY]
for example:
chris :0
I can do it with:
for sessionid in $(/bin/loginctl list-sessions --no-legend | awk '{ print $1 }'); do
       /bin/loginctl show-session -p Display -p Name --value $sessionid | sed ':a;N;s/\n/ /'
done
BUT I need my script is used by root (suspend script in /etc/elogind/system-sleep/), and that's why I need to know user and display to use. And when used by root (script), /bin/loginctl list-sessions seems to not give any result. (used by root in a terminal works well) What is the best, robust way to achieve this please? Currently, I use:
if [ -S /tmp/.X11-unix/X* ] ; then
         display=":$(ls /tmp/.X11-unix/X* 2>/dev/null | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
         if [ -n "$display" ]; then
                 user=$(who | grep '('$display')' | awk '{print $1}' | sort -u | head -n1)
                 [ -n "$user" ] && echo $user $display
         fi
fi
But I don't find it really very clean, even if it works very well for me.
Asked by Chris972 (43 rep)
Nov 13, 2023, 05:16 PM