You are not logged in.
( Following on from here: https://forums.bunsenlabs.org/viewtopic.php?id=2673 )
WHY: I've been searching for a way for root processes to send messages to all logged-in users on X sessions.
The specific case in mind was a systemd timer, calling a systemd service. I wanted some notification if the service failed, using 'notify-send' for a nice-looking unobtrusive popup, but I think this script might be useful for anyone who wants notifications from root-run scripts, like cron jobs for example.
The previous script using 'w -hs' wasn't catching users who'd started X sessions via a tty and 'startx' (unless they happened to have a terminal open) but this one using 'ps e' seems to be sending messages to every user on every display. I've skipped "root" from the recipients, but it would be easy to include if people thought it was a good idea.
SETUP:
Please copy the code below into a file, and as root, put it in /usr/bin/notify-broadcast, and make it executable. ('sudo chmod +x /usr/bin/notify-broadcast') If the previous notify-broadcast is still around you can overwrite it.
Here's the script:
#!/bin/bash
HELP="Call notify-send for all current X users.
Passed arguments are sent on as-is.
This script must be run as root."
required_commands=(notify-send) #array
error_exit() {
echo "$0 error: $1" >&2
exit 1
}
missing_commands=
for i in "${required_commands[@]}"
do
hash $i || missing_commands+=" $i"
done
[[ $missing_commands ]] && error_exit "This script requires the following commands: $missing_commands
Please install the packages containing the missing commands
and rerun the script."
case $1 in
--help|-h)
echo "$HELP"
exit
;;
esac
[[ $( id -u ) -eq 0 ]] || error_exit "This script must be run as root."
declare -A disps usrs
usrs=()
disps=()
for i in $(users);do
[[ $i = root ]] && continue
usrs[$i]=1
done # unique names
for u in "${!usrs[@]}"; do
for i in $(ps e -u "$u" | sed -rn 's/.* DISPLAY=(:[0-9]*).*/\1/p');do
disps[$i]=$u
done
done
for d in "${!disps[@]}";do
sudo -u "${disps[$d]}" DISPLAY="$d" notify-send "$@"
done
exit
EDIT: code slightly changed, but it shouldn't affect anything.
TEST:
Try any of the commands below from a terminal, and report if the popup message appeared:
notify-broadcast --help # should output a short message, and exit 0
notify-broadcast # should output an error message and exit 1
sudo notify-broadcast 'Title' 'Any message you like' --icon=dialog-error # or some other icon
sudo notify-broadcast ...any notify-send arguments you like... # all should work
If you have a root password, open a new tty (Ctrl+Alt+2 or 3...), login as root and try some of the same commands. If you switch quickly back to tty7 (Ctrlk+Alt+7) you should see the notification popup.
Especially if you have (or can easily put together) a multi-user setup, either with different ttys on one machine or with physically separate display screens, please check if the message gets sent to all of them (X sessions, not terminals).
Technical discussion here: https://forums.bunsenlabs.org/viewtopic.php?id=2685
Many thanks!
Last edited by johnraff (2016-09-20 07:12:45)
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
TEST:
Try any of the commands below from a terminal, and report if the popup message appeared:notify-broadcast --help # should output a short message, and exit 0 notify-broadcast # should output an error message and exit 1 sudo notify-broadcast 'Title' 'Any message you like' --icon=dialog-error # or some other icon sudo notify-broadcast ...any notify-send arguments you like... # all should work
Hi, John -- I tried the first three and they worked. Forgive me, but despite the fact I should know at least one notify-send argument, I am not sure what you mean (that's on me, though, because I'm not very good at programming). However, on the fourth one where it says "...any notify-send arguments you like..." I ended up just typing text and it worked, though I don't know if that's the result you were expecting.
Res publica non dominetur
Offline
I only have a laptop here but I created a new user,
switched to tty4, logged in and ran startx.
Switched back to tty7 and ran sudo notify-broadcast.
Icon , title and message showed up in both tty7 and 4.
It also notifies in tty7 if new user runs it in tty4.
Inputting your commands it displays the help message
and exits 1 when no arguments are given.
( /usr/bin/notify-broadcast error: This script must be run as root. )
FWIW, the original worked as you said, i.e. it didn't unless
new user had a terminal open in tty4.
Offline
@username many thanks for the confirmation.
@lcafiero thanks for checking. The arguments notify-broadcast gets are passed on to notify-send as-is, so the same as 'man notify-send' specifies.
But anyway, the main thing I'm interested in here is if the messages are successfully broadcast to all the users running X sessions. It's looking hopeful so far...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
I think it's probably OK, but if we had, say one more confirmation it would be cool:
Especially if you have (or can easily put together) a multi-user setup, either with different ttys on one machine or with physically separate display screens, please check if the message gets sent to all of them (X sessions, not terminals).
I'm going to install an ssh server and try the commands from another machine, too. If anyone already has an ssh connection set up and can do a remote login and try a 'notify-broadcast' that would be nice too, and save me the trouble...
Anyway, thanks all!
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
@johnraff,
I'm not sure if this is what you want, but...
I have a laptop running windows. I'd completely forgotten
I have putty installed there, it's been so long since I used it.
Anyway, notify-broadcast sent via putty, both users get notified
on the old piece of junk running BL (beautifully, thanks)
Of course this is ssh-ing via username@local ip, (the laptops are
only 6 feet apart) but I assume this means it will work ssh-ing
in from outside the network too.
Apologies if this is not what you were after.
Offline
The new script works as expected with the messages showing on all user's screens, even with no terminals open.
If I can manage to wrestle my other laptop away and set up SSH (and figure out how to use it) I will try that later.
Offline
@username thanks, that's exactly what I wanted!
@HoaS if you haven't got ssh set up already, please don't go to all that trouble, I think we've now got enough input to say it's probably OK.
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
Marked OP as [DONE].
Thanks everyone!
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )
Offline
Just bumping this 'cos I got a nice little pop-up message while I was browsing (top right of screen):
Offline