Last active
May 19, 2024 17:19
-
-
Save jesustorresdev/e8810dbceece820b4ae5aa0ee5ca200a to your computer and use it in GitHub Desktop.
Script for the root to send desktop notifications to all active sessions
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
#!/bin/bash | |
# | |
# notify-send-all - Script to send desktop notifications to all active sessions | |
# | |
# https://gist.github.com/aplatanado/e8810dbceece820b4ae5aa0ee5ca200a | |
# | |
### Utility functions | |
# A few utility functions to show errors, handle programa exit and more | |
PROGRAM_NAME="$(basename "$0")" | |
function error_exit() { | |
echo "${2:-"Unknown error"}" 1>&2 | |
exit "${1:-1}" | |
} | |
### Loginctl commands | |
# Functions to make it easy to invoke some loginctl commands | |
function list-sessions() { | |
loginctl list-sessions --no-pager --no-legend | awk '{print $1}' | |
} | |
function show-session() { | |
local session_id=$1; shift | |
local property loginctl_params | |
for property in $@; do | |
loginctl_params="$loginctl_params -p $property" | |
done | |
loginctl show-session --no-pager --no-legend $loginctl_params $session_id | |
} | |
### Main function | |
function notify-send-all() { | |
local session_id | |
local Active Display Name | |
test "$UID" -eq 0 || | |
error_exit 1 "You must be root to use this tool." | |
type -P notify-send &> /dev/null || | |
error_exit 2 "The command notify-send was not found." | |
for session_id in $(list-sessions); do | |
eval $(show-session $session_id Active Display Name) | |
if [[ "$Active" == "yes" ]]; then | |
su "$Name" -c "DISPLAY=$Display notify-send "'"$@"' -- "$@" | |
fi | |
done | |
exit 0 | |
} | |
notify-send-all "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment