Created
June 8, 2020 01:43
-
-
Save D-Nice/537b32842a9f15d091009c8456a3849e to your computer and use it in GitHub Desktop.
A posix shell compliant script for sending a notification to all logged in users with a display in use (tested on void, put in an sbin path, needs to run as root)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
verbose_echo() | |
{ | |
[ -n "${VERBOSE##[NFnf]*}" ] && \ | |
echo "$@" | |
} | |
LOGGED_USERS=$(who | awk '{print $1 " "}') | |
for user in $LOGGED_USERS; do | |
# grab the user's running DISPLAY | |
disp=$(ps -u "$user" -o pid= | | |
xargs -I{} cat /proc/{}/environ 2>/dev/null | | |
tr '\0' '\n' | | |
grep -m1 '^DISPLAY=') | |
# check used X DISPLAY exists for the logged in user | |
if [ -n "$disp" ]; then | |
verbose_echo attempting to notify "$user" on "$disp" | |
# run notify-send with passed command | |
sudo -u "$user" "$disp" /bin/notify-send "$@" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment