Notifications from root via shell script

Asked by jhansonxi

What is the proper way to send notifications to X terminals on Ubuntu 12.04 (Precise Pangolin)?

The notifications are used for the following:

ssh: Notification to the remote user that an administrator has connected so don't panic and pull the plug.

openvpn: Notification to the administrator that a remote client has connected to allow a return ssh session for administration through a restrictive firewall.

smartd: Notification of drive failures.

mdadm: Notification of RAID events.

I'm using Linux Mint 13 (maya) in a gnome-fallback session since my test hardware (i855GM Intel chipset) doesn't support usable 3D but I need this to work with any system with Cinnamon and Unity also.

On Ubuntu 10.04 (Lucid Lynx) I had to go tty hunting with ps then extract the DISPLAY variable from /proc/<pid>/environ, then set the variable for root and just send a message with notify-send. Complicated but it worked.

Note that I can't use ck-list-sessions as notifications need to be sent to the display manager if no users are logged in. The DM session does not show up in ck-list-sessions.

With 12.04, the DISPLAY variable is not in the environment of the process that is connected to the tty. I've worked around this by extracting the display setting in the X command line from ps args. Not robust but it's the least bad method I've found.

However, this isn't enough to get notify-send functioning. I figured out that DISPLAY has to be set, notification-daemon started for root, then notify-send used to send the message. This cycle has to be repeated for every X terminal.

In addition, there are timing bugs with this approach. I have to add in sleep between launching the daemon and sending the message else it doesn't work. Even then, the notification often disappears abruptly, long before the timeout expires. I had this problem with Lucid also but adding a short sleep before terminating the script solved the problem. It's worse on Precise as it occurs even if the script hasn't terminated.

In general, this makes the script unreliable and slow. Is there a better method available using D-bus or something?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu libnotify Edit question
Assignee:
No assignee Edit question
Solved by:
actionparsnip
Solved:
Last query:
Last reply:
Revision history for this message
Best actionparsnip (andrew-woodhead666) said :
#1

You can use notify-osd. If you export DISPLAY as 0.0 then the commands you run will appear on the server you are connecting to.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#2

or you can use wall, depends if the users sit in a command prompt

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#3

You could have a script that takes an arg to display. Eg:

#!/bin/bash
export DISPLAY=:0.0
notify-send "Message:" "$($1)"

You can then run:

ssh user@server inform test

Should work afaik.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#4
Revision history for this message
jhansonxi (jhansonxi) said :
#5

#1 - notify-osd doesn't provide any benefit over notification-daemon. While it is somewhat compatible, it has several limitations and no command-line interface of its own. It just uses notify-send:
https://wiki.ubuntu.com/NotificationDevelopmentGuidelines#Help.21_No_libnotify_bindings_for_language_X

#2 - I already use wall for tty notifications.

#3 - My scripts already do that. SSH isn't useful as the notifications are local.

#4 - I'd use dialogging apps as a last resort only. Notifications should be non-interactive. None of Zenity, kdialog, Xdialog, xmessage, gxmessage, whiptail, and dialog properly replace notification balloons.

Revision history for this message
jhansonxi (jhansonxi) said :
#6

Thanks actionparsnip, that solved my question.

Revision history for this message
jhansonxi (jhansonxi) said :
#7

(stupid default solution message)
Actually, notify-osd does seem more reliable than notification-daemon and I don't have to start a separate daemon for root for each display. I'll have to test it some more to see what breaks.