11

Is it possible to use notify-send to send messages to other users on the system?
preferably to a specified user, but system wide would work aswell.

Ubuntu/Debian with libnotify installed

  • See this answer for an example that works with Ubuntu 16.04, even from a script started by root in crontab. – mivk Feb 12 '17 at 11:44
6

If you have the other users password you can do sudo -u somedude notify-send Hello. somedude must have an X-session started.

Edit: Found this script for use with cron:

#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin
export DISPLAY=:0.0
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -session)/environ )
TIME=$1
shift
/usr/bin/notify-send -t 36000 $1 $2
  • with slight modification, that script does what i need it to do, thanks – madmaze Oct 5 '11 at 16:49
  • didn't you miss one hyphen before -session? – vladkras Oct 19 '16 at 9:05
  • no @vladkras pgrep is supposed to be getting the pid of the X-session manager, i.e. lxde-session, gnome-session etc. That script is a crude hack at best and depends a lot on your system setup. It worked when I answered but doesn't work without modification now, 5 years later. – micke Oct 21 '16 at 12:23
  • It doesn`t work for me either – vladkras Oct 22 '16 at 4:23
2

If the users are on terminals the following commands will be of use -

write user [tty]

or to send to all users

wall
1

I stumbled upon the following project that implements a user notify: tinynotify-send

0

I needed similar tool to send notification to all sessions. Here is my solution: https://unix.stackexchange.com/a/230062/93227

It scans /proc to find all session busses and then it executes notify-send on each of it (once per bus). All arguments are passed unchanged to real notify-send.

#!/bin/bash

/bin/grep -sozZe '^DBUS_SESSION_BUS_ADDRESS=[a-zA-Z0-9:=,/-]*$' /proc/*/environ \
| /usr/bin/php -r '
        $busses = array();
        array_shift($argv);
        while($ln = fgets(STDIN)) {
                list($f, $env) = explode("\0", $ln, 2);
                if (file_exists($f)) {
                        $user = fileowner($f);
                        $busses[$user][trim($env)] = true;
                }
        }
        foreach ($busses as $user => $user_busses) {
                foreach ($user_busses as $env => $true) {
                        if (pcntl_fork()) {
                                posix_seteuid($user);
                                $env_array = array("DBUS_SESSION_BUS_ADDRESS" => preg_replace("/^DBUS_SESSION_BUS_ADDRESS=/", "", $env));
                                pcntl_exec("/usr/bin/notify-send", $argv, $env_array);
                        }
                }
        }
' -- "$@"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.