Sample Header Ad - 728x90

How do I request a user input with GUI from script running as root by modern initialization system?

1 vote
0 answers
47 views
I have Systemd service of type "oneshot" to be launched by Udev upon a device insertion. Service is launching shell script as root. The script needs user input in order to do it's job as intended. User is logged into GUI session (it's Xorg for now and a while, but not obligatory). How do a process running as root may request an input from a regular, current active user running GUI session? What about if it's sensitive data or when data is not? And what about if it's **not** Xorg? One way to get data into variable is the following part of the script:
#!/bin/bash
while read session_id login ; do
    if [ "$( loginctl show-session -p State "${session_id}" )" == "State=active" ]
    then
        break
    fi
done <<< "$( loginctl list-sessions --no-legend | awk '{print $1 " " $3}' )"

if [ -n "${login}" ] ; then
    string="$(  DISPLAY=:0.0 \
            XAUTHORITY="/home/${login}/.Xauthority" \
            zenity --entry )"
else
    echo "WARNING:${0}: Do nothing as no one logged in."
    exit
fi
But it needs complex evaluation of DISPLAY, for example. And it's Xorg only? What is a best way to do it with regular tools for Linux? Thank you!
Asked by Asc (11 rep)
Sep 1, 2023, 09:29 PM
Last activity: Sep 1, 2023, 09:51 PM