Created
June 14, 2020 17:09
-
-
Save Thrilleratplay/aa8d6e2b8ee4c4a939d109743e07171c to your computer and use it in GitHub Desktop.
Send notification to all users with x11 sessions. Useful for sending notifications from scripts that are run as root.
This file contains 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
#!/usr/bin/env bash | |
# Send notification to all users with x11 sessions. Useful for sending notifications | |
# from scripts that are run as root. | |
# | |
# | |
# EXAMPLES: | |
# | |
# $> notify-send-all-users.sh Hello! | |
# | |
# $> notify-send-all-users.sh 'No running by the pool' --icon=dialog-warning | |
# | |
# $> notify-send-all-users.sh 'This text in bold' 'You failed to fail successfully' --icon=dialog-error | |
# | |
# | |
# Sources: | |
# | |
# https://stackoverflow.com/questions/28195805/running-notify-send-as-root/49533938#49533938 | |
# https://forums.bunsenlabs.org/viewtopic.php?id=2685 | |
function notify-send() { | |
local display | |
local user | |
local uid | |
# list of open senssions | |
sessions=$(loginctl --no-legend list-sessions | sed -e 's/^ *//g'| cut -f1 -d " ") | |
for sessionid in $sessions; | |
do | |
# If user is logged in to X11 | |
if [[ $(loginctl --property Type --value show-session "$sessionid") = 'x11' ]]; then | |
# determine username, uid and display the used by user | |
user=$(loginctl --property Name --value show-session "$sessionid"); | |
uid=$(loginctl --property User --value show-session "$sessionid"); | |
display=$(loginctl --property Display --value show-session "$sessionid"); | |
# display notification to that user | |
sudo -u "$user" DISPLAY="$display" DBUS_SESSION_BUS_ADDRESS=unix:path="/run/user/$uid/bus" notify-send "$@" | |
fi | |
done | |
} | |
notify-send "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works with wayland, just have to check for X11 or wayland,
I added a send to the user tty if no display session exist.