2

I'm working on a bash script which requires root privileges, but which I'd like to give the ability to notify a non-root user of certain alerts via notify-send in GNOME (or any other desktop shell supporting that command). As expected, simply invoking notify-send from a root shell doesn't display a notification on the non-root user's desktop.

I've tried to invoke the command via:

su -c "notify-send 'my message'" -s /bin/sh someuser

(as well as using /bin/bash, /usr/zsh), but neither of these has had any effect.

How can I invoke notify-send from a root shell so that it shows a notification on a (specified) non-root user's desktop?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Jules
  • 2,084

1 Answers1

0

You have to do something like this:

DISPLAY=:0.0 su $USER -c "/usr/bin/notify-send 'my message'"

The above is how I have done in the past with cron jobs that are run as root. Of course you need to specify an actual user in the $USER variable.

  • 2
    This approach doesn't seem to work under GNOME 3.22.1 with notify-send 0.7.7 running on Arch Linux (kernel 4.8.6). Can you confirm whether, in past cases, it's worked with applications which affect a GUI desktop environment? – Jules Nov 13 '16 at 09:13
  • I use this in a cron job under Debian with Xfce as desktop environment. I have no machine with Gnome to test on unfortunately. –  Nov 13 '16 at 09:42
  • Works on xfce mint as well. – Eric Sebasta Jan 13 '21 at 21:40