You are not logged in.

#1 2011-12-30 22:46:12

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

GUI notifications from privileged scripts?

I have various scripts which use a facility to trigger a GUI popup notification under certain conditions. Many of these scripts are run with root privileges but I need the popups to appear in a user's X session. I'm trying to migrate these scripts from OS X to Linux but I've run into some problems. I have found various different ways of getting popup notifications and these work fine when I run the command or script myself from the command line but they fail if run as root at the command line or if included in a script running with privileges. (I haven't tried scheduled non-privileged scripts yet as the privileged ones are the most important.)

For example, I set smartmontools up so that the smartd daemon will popup a notification if it thinks a drive it is monitoring may be failing. I'm particularly keen to get this one working as I am pretty sure that on my old laptop I would have lost data if it weren't for the popup alerts from smartd.

I can tell from the logs that my script is getting run - I have smartd.conf set up with the test option so I can see if the notifications are working. But I'm not getting the notifications.

Initially, I tried to use notify-send in the same way that I would have used growl under OS X. However, I read that root does not have automatic access to the GUI of users under X so I decided to try using sudo or su to run the command as my regular user. I've also tried doing this with kdialog because I'm using KDE. (But I have the same problem under XFCE with Debian on my old laptop.)

/bin/su -c '/usr/bin/notify-send  -u critical -t 100000 -i /etc/SMART-alert.png "SMARTD ALERT MESSAGE" "$SMARTD_DEVICE MAY BE FAILING"' <username>
/bin/su -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --passivepopup "$SMARTD_DEVICE MAY BE FAILING" ' <username>

I initially tried sudo but switched to su when that didn't work.

I wondered if notify-send and/or kdialog were being treated as graphical applications and if I maybe needed to try graphical sudo. This doesn't quite make sense because that's usually used by a user who is in control of the overall GUI to run a particular app as another user but, hey, it seemed worth a shot:

$(kde4-config --path libexec)kdesu -c '/usr/bin/notify-send  -u critical -t 100000 -i /etc/SMART-alert.png "SMARTD ALERT MESSAGE" "$SMARTD_DEVICE MAY BE FAILING"' <username>
$(kde4-config --path libexec)kdesu -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --error "$SMARTD_DEVICE MAY BE FAILING"' <username>

I'm pretty sure this isn't right because it spewed errors whereas my original method just did nothing. (Or maybe errors are a good sign?!)

I also appear to have tried the following although I am not now sure why:

/usr/bin/wall<<EOF
$SMARTD_DEVICE MAY BE FAILING
EOF

I believe this was especially unlikely looking so I abandoned this.

Today I've been trying the su notify-send/kdialog method with various different tweaks. For example, a hint I found online suggested:

xhost +local:root

(I think the root part does nothing - it just allows local connections generally.)

I also tried:

export $(dbus-launch)

when an error message from one of my attempts with kdesu suggested this.

Here's an example line from the logs from a smartd test run when I was trying to use sudo:

root : TTY=unknown ; PWD=/ ; USER=<username> ; COMMAND=/usr/bin/notify-send -u critical -t 100000 -i /etc/SMART-alert.png SMARTD ALERT MESSAGE /dev/sda MAY BE FAILING

Running the command from the command line as root with sudo, I get a specified TTY but there is still no notification. (sudo is just getting attempts into the logs because it is sudo - it doesn't seem to be indicating what or, even, that anything is wrong!)

I can't help feeling that I'm making this much more complicated than I need to. I'd be really grateful if somebody could point me in the right direction.

Edit: for reasons I don't understand, this is now working when I issue the commands from the command line as root. But it still doesn't work when triggered by smartd...

Last edited by cfr (2011-12-30 22:53:56)


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#2 2011-12-31 09:10:17

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: GUI notifications from privileged scripts?

Might or might not help you, but I'm using notify-send from a script launched as root by rc.local.

In rc.local:

DISPLAY=":0.0" /etc/openvpn/launch_vpn

In launch_vpn:

function notify
{
	su stqn -c "notify-send $1 \"$2\""
}

_My_ problem is that although this works great for notify-send, it doesn't for mousepad...

Last edited by stqn (2011-12-31 09:11:34)

Offline

#3 2011-12-31 10:18:49

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,458

Re: GUI notifications from privileged scripts?

@cfr:
Why you want the notification to have root privileges? it's useless.

#!/bin/bash
DISPLAY=:0.0
xuser=$(who|grep ":0"|cut -f 1 -d " "|tail -n 1)
sudo -u $xuser kdialog --sorry "There is an error"

This is used by root which became user and notify.

Last edited by kokoko3k (2011-12-31 10:20:42)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#4 2011-12-31 17:30:56

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

stqn wrote:

In rc.local:

DISPLAY=":0.0" /etc/openvpn/launch_vpn

Thank you _very_ much! I was sure I'd tried setting DISPLAY...

I can now get it to work with kdialog but still not with notify-send. (Maybe I tried setting DISPLAY only for notify-send...)

DISPLAY=":0.0" /bin/su -c '/usr/bin/notify-send  -u critical -t 100000 -i /etc/SMART-alert.png "SMARTD ALERT MESSAGE" "$SMARTD_DEVICE MAY BE FAILING"' <username>
DISPLAY=":0.0" /bin/su -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --passivepopup "$SMARTD_DEVICE MAY BE FAILING"' <username>
DISPLAY=":0.0" /bin/su -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --error "$SMARTD_DEVICE MAY BE FAILING"' <username>

This now gets me two alerts but the first still shows nowt...


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#5 2011-12-31 19:02:56

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

I've also noticed that the variable DISPLAY seems to be set to :0 rather than :0.0 in my environment. Does that mean I should be using :0 in the scripts? Or is something wrong with the way my environment is configured?


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#6 2011-12-31 19:04:50

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

kokoko3k wrote:

Why you want the notification to have root privileges? it's useless.
.

Just to be clear: I don't want the notification to have root privileges. Just the script that initiates the notification needs to be run with root privileges.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#7 2012-01-01 01:42:57

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

I'm getting more and more confused by this. In order to experiment without continually stopping and starting the smartd daemon, I copied the relevant script to a test file and edited the content to concentrate on the lines of interest:

#!/bin/sh -
set -x

DISPLAY=":0.0" /bin/su -c '/usr/bin/notify-send  -u critical -t 100000 -i /etc/SMART-alert.png "SMARTD ALERT MESSAGE" "$SMARTD_DEVICE MAY BE FAILING"' <username>
/bin/su -c 'DISPLAY=":0.0" /usr/bin/notify-send  -u critical -t 100000 -i /etc/SMART-alert.png "SMARTD ALERT MESSAGE" "$SMARTD_DEVICE MAY BE FAILING"' <username>
DISPLAY=":0.0" /bin/su -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --passivepopup "$SMARTD_DEVICE MAY BE FAILING"' <username>
DISPLAY=":0.0" /bin/su -c '/usr/bin/kdialog --icon /etc/SMART-alert.png --title "SMARTD ALERT MESSAGE" --error "$SMARTD_DEVICE MAY BE FAILING"' <username>

# And exit
exit 0

I added the set -x to try to get some useful output. I expected:

./test

run as root to fail. After all, isn't that just what the relevant line in my smart configuration file is effectively doing?

But it works. I get three passive popus - two presumably from notify-send and one from kdialog. And then I get the kdialog error box, too. Curiously, the kdialog passivepopu now looks and behaves like the notify-send popus do. Usually, that one appears centre screen and has a different "look".

So now I have even less idea what is going on. I hoped this would throw some light on things. Instead the mystery has just grown murkier still...


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#8 2012-01-01 08:20:54

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: GUI notifications from privileged scripts?

Try

su -c 'env DISPLAY=0:0 <command>' <user>

Offline

#9 2012-01-01 16:15:04

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

some-guy94 wrote:

Try

su -c 'env DISPLAY=0:0 <command>' <user>

Thanks for the suggestion but it didn't make any difference.

What's odd (to me) is that the commands in the script succeed. If I add echo $? after the notify-send commands, I get 0 in each case. But nothing appears on the desktop... And I just can't understand why they work in a script run by root from the command line but not in the script triggered by smartd which is essentially a script run by root, as far as I can tell... I'm also not sure what difference there is between kdialog and notify-send which allows the former but not the latter to succeed...


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#10 2012-01-01 20:50:14

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: GUI notifications from privileged scripts?

Odd. I think I actually had a similar problem with cron jobs, and what I ended up doing is making a script that looks like

#!/bin/bash

trap "rm -f /tmp/notifyd.$USER" EXIT SIGINT SIGHUP

[[ -e "/tmp/notifyd.$USER" ]] && rm -f "/tmp/notifyd.$USER"
mkfifo "/tmp/notifyd.$USER"
while read line <"/tmp/notifyd.$USER"; do
    notify-send "Cron Job" "$line"
done

and just sent the messages to /tmp/notifyd.$USER.

Though that's not the most elegant script/solution on earth.

Last edited by some-guy94 (2012-01-02 18:50:35)

Offline

#11 2012-01-02 15:08:41

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

Thanks! I can't make this work right now but I think that's just my ignorance. Your solution just outstrips my shell scripting knowledge so I'll need to look into it in more detail in order to make use of it.

Wish I knew why the solution was necessary, though!


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#12 2012-01-02 19:03:14

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: GUI notifications from privileged scripts?

If you use that script, you'll need to send your notification by doing echo "text" >"/tmp/notifyd.$USER"
The script just makes a FIFO and sends each line it reads from it as a notification.

The reason it doesn't work could be because of a dbus issue.
Try adding 'source $HOME/.dbus/session-bus/*' to the top of your scripts.

Offline

#13 2012-01-02 20:40:49

b4data
Member
Registered: 2010-12-14
Posts: 141

Re: GUI notifications from privileged scripts?

Instead of using su for this reason, I've had success with using this as root:

sudo -u <username> -H notify-send <args>

Offline

#14 2012-01-03 00:27:24

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,168

Re: GUI notifications from privileged scripts?

Thanks. I was trying the echo bit. Didn't know about the dbus issue, though, so I'll give that a shot.

I tried sudo to start with but I maybe didn't try the -H option so I'll see what that does as well. Thanks.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#15 2012-01-04 05:03:21

greyscale
Member
From: Sweden
Registered: 2011-01-02
Posts: 59

Re: GUI notifications from privileged scripts?

Think you need the username, $DISPLAY and $DBUS_SESSION_BUS_ADDRESS of the user you want to notify.

Try doing something like this (but change awesome into the process name of the window manager your running):

#!/bin/bash                                                                                                              
                                                                                                                         
WMNAME=awesome                                                                                                           
WMPID=$(pidof ${WMNAME})                                                                                                 
WMUSER=$(ps -e -o user,comm | grep ${WMNAME} | cut -f1 -d " ")                                                           
DBUS=$(egrep -z 'DBUS_SESSION_BUS_ADDRESS|DISPLAY' /proc/${WMPID}/environ | sed -r -e 's/(.)DBUS_/\1 DBUS_/' -e 's/(.)DISPLAY/\1 DISPLAY/')
                                                                                                                         
/bin/su ${WMUSER} -s /bin/bash -c "${DBUS} notify-send -t 100000 -u critical ErrorMsg" 

Haven't used it myself but might be something for you
https://github.com/mgorny/libtinynotify-systemwide


A tiling window manager simply manages your windows, not just letting you have em
:: Configs etc @GitHub

Offline

Board footer

Powered by FluxBB