notify-send-broadcast

Because I wanted me to receive graphical notifications from the scripts laptop-mode-tools a while ago I searched the net an found some solutions, e. g. here or here, which enabled me two write a script to make this possible. This is what I share today. I wrote about this in german, but now and here it’s for english fellows.

The script below determines the running X-sessions (as configured) and sends a notification to each of them. This requires root priviledges.

Combined with laptop-mode tools you can easily setup user notifications in the file /etc/laptop-mode/conf.d/exec-commands.conf reporting things like whether there is an AC-cable attached or not.

#!/bin/bash
########################################################################
#    Name: notify-send-broadcast, Author: Henning Hollermann,          #
#    laclaro@mail.com                                                  #
#                                                                      #
#    This program is free software: you can redistribute it and/or     #
#    modify it under the terms of the GNU General Public License       #
#    as published by the Free Software Foundation, either version 3    #
#    of the License, or (at your option) any later version             #
#                                                                      #
#    This program is distributed in the hope that it will be useful,   #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of    #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      #
#    GNU General Public License for more details.                      #
#                                                                      #
#    You should have received a copy of the GNU General Public License #
#    along with this program. If not, see <http://gnu.org/licenses/>.  #
#                                                                      #
#    Bash-Script to send broadcasts to all users to the desktop-       #
#    notification-daemon root permissions are required to call         #
#    notify-send from another users account                            #
#                                                                      #
#    Written in bash using getopts for options processing.             #
#    Type "notify-send-broadcast -h" to get usage Information.         #
#                                                                      #
########################################################################

sessionarray="openbox"       # i.e. "gnome-session openbox startkde"

[ "x$DISPLAY" = "x" ] && export DISPLAY=:0.0

# Defaults
title="No Title"
# message-timeout in milliseconds
# (only with notification-daemon or modified notify-osd)
timeout="2000"
icon="foo.bar"
urgency="normal"

# Process options with getopts
while getopts ":t:m:i:T:u:vh" opt; do
  case $opt in
    # setting title, else fall back to "no title"
    "t")  title=$OPTARG;;
    # message text (optional)
    "m")  message=$OPTARG;;
    # display-time in milliseconds (optional)
    "T")  timeout=$OPTARG;;
    # notification icon (optional), could be i.e.
    # "battery", "sonata", "network" or an icon path.
    "i")  icon=$OPTARG;;
    # notification urgency-level, default= normal
    "u")  urgency=$OPTARG;;
    "v")  echo -e "notify-send-wall: Written in bash, using getopts.
Made by Henning Hollermann 2011/10, GPL v3 License.";
        exit 0;;
    "h")  echo -e "Usage: $0 -t [options]\nOptions:\n  -t\tSet Title\n
  -m \tSet Message to Display\n
  -T \tSet Display-Time in milliseconds (default: 2000ms)\n
  -i\t\tNotification icon. Could be something like battery-charging,
sonata, network or an icon file path. \n  -u\t\tSet urgency-level of
message (low|normal|critical)\n  -h\t\tDisplay this help message\n
  -v\t\tDisplay version information";
        exit 0;;
    \?) echo -e "Invalid option: -$OPTARG\n$help" >&2;
        exit 1;;
    ":") echo "Option -$OPTARG requires an argument." >&2;
         exit 1;;
  esac
done

# Send notification to all sessions
for session in $sessionarray; do
    pids=$(pgrep $session)
    for pid in $pids; do
        # Determine session owner
        user=$(stat -c '%U' /proc/$pid)
        # send notification to session (must be executed by $user: root-permissions sequired)
        sudo -u $user DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') /usr/bin/notify-send -i "$icon" -u $urgency -t "$timeout" "$title" "$message"
    done;
done

# Logging
#logger "sudo -u $user DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') /usr/bin/notify-send -i "$icon" -u normal -t "$timeout" "$title" "$message""

exit 0;
About

Peter Pan. Kann fliegen mit Feenstaub.

Tagged with: , , ,
Posted in Linux, Technik
One comment on “notify-send-broadcast
  1. […] but I have used it for years in conjunction with laptop-mode-tools (as power-saving features) and notify-send-broadcast (if you want to run it as startup-script as root) and never needed anything […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Posts by topic…
…by month
Have a look at…


%d bloggers like this: