-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas-gui-user.sh
executable file
·55 lines (43 loc) · 1.22 KB
/
as-gui-user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Simple helper to run a command as all users that are currently running a
# graphical environment, with proper DBUS session environment variables set.
# This is useful for sending desktop notifications and works with notify-send.
IFS=
displays=()
resolve_displays() {
local display
while read display; do
[[ "${display:0:2}" = "(:" ]] || continue
displays+=( $display )
done < <(who | awk '{ print $5 }' | sort -u)
}
call_as_user() {
local user="$1"
local display="${2:1:-1}" # slice away the parentheses
local uid=$(id -u $user)
shift 2
/usr/bin/sudo \
--user=$user \
DISPLAY=$display \
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus \
"$@"
}
call_as_all_users() {
local user
local display
for display in ${displays[@]}; do
user=$(who | grep -G "$display" | awk '{ print $1 }' | head -n1)
[[ "$user" ]] || continue # should always hold
call_as_user $user $display "$@"
done
}
if [[ $# = 0 ]]; then
echo "usage: ${0##*/} [command] [args...]"
exit 0
fi
resolve_displays
if [[ ${#displays[@]} = 0 ]]; then
echo "No graphical environments found."
exit 1
fi
call_as_all_users "$@"