Advertisement
laclaro

notify-send-wall

Oct 25th, 2011
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.20 KB | None | 0 0
  1. #!/bin/bash
  2. #####################################################################################
  3. #    Author: laclaro@arcor.de                                                       #
  4. #                                                                                   #
  5. #    This program is free software: you can redistribute it and/or modify           #
  6. #    it under the terms of the GNU General Public License as published by           #
  7. #    the Free Software Foundation, either version 3 of the License, or              #
  8. #    (at your option) any later version.                                            #
  9. #                                                                                   #
  10. #    This program is distributed in the hope that it will be useful,                #
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of                 #
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  #
  13. #    GNU General Public License for more details.                                   #
  14. #                                                                                   #
  15. #    You should have received a copy of the GNU General Public License              #
  16. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.          #
  17. #                                                                                   #
  18. #    Bash-Script to send broadcasts to all users to the desktop-notification-daemon #
  19. #    root permissions are required to call notify-send from another users account   #
  20. #                                                                                   #
  21. #    Written in bash using getopts for options processing.                          #
  22. #    Type "notify-send-wall -h" to get usage Information.                           #
  23. #                                                                                   #
  24. #####################################################################################
  25.  
  26. sessionarray="openbox"       # i.e. "gnome-session openbox startkde"
  27.  
  28. [ "x$DISPLAY" = "x" ] && export DISPLAY=:0.0
  29.  
  30. # Defaults
  31. title="No Title"
  32. timeout="2000"              # message-timeout in milliseconds (only with notification-daemon or modified notify-osd)
  33. icon="foo.bar"
  34. urgency="normal"
  35.  
  36. # Process options with getopts
  37. while getopts ":t:m:i:T:u:vh" opt; do
  38.   case $opt in
  39.     t) title=$OPTARG;;      # setting title, else fall back to "no title"
  40.     m) message=$OPTARG;;    # message text (optional)
  41.     T) timeout=$OPTARG;;    # display-time in milliseconds (optional)
  42.     i) icon=$OPTARG;;       # notification icon (optional), could be i.e. "battery", "sonata", "network" or an icon path.
  43.     u) urgency=$OPTARG;;    # notification urgency-level, default= normal
  44.     v) echo -e "notify-send-wall: Written in bash, using getopts. Made by Henning Hollermann 2011/10, GPL v3 License."; exit 0;;
  45.     h) echo -e "Usage: $0 -t <title> [options]\nOptions:\n  -t <title>\tSet Title\n  -m <msg>\tSet Message to Display\n  -T <time>\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;;
  46.     \?) echo -e "Invalid option: -$OPTARG\n$help" >&2; exit 1;;
  47.     :) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
  48.   esac
  49. done
  50.  
  51. # Send notification to all sessions
  52. for session in $sessionarray; do
  53.     pids=$(pgrep $session)
  54.     for pid in $pids; do
  55.         # Determine session owner
  56.         user=$(stat -c '%U' /proc/$pid)
  57.         # send notification to session (must be executed by $user: root-permissions sequired)
  58.         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"
  59.     done;
  60. done
  61.  
  62. # Logging
  63. #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""
  64.  
  65. exit 0;
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement