Skip to content

Instantly share code, notes, and snippets.

@D-Nice
Created June 8, 2020 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D-Nice/537b32842a9f15d091009c8456a3849e to your computer and use it in GitHub Desktop.
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)
#!/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