I would like to give out a notification on screen by a script that is running by root on Ubuntu 14.04.
I tried this:
#!/bin/bash
MAINUSER=$(cat /etc/passwd|grep 1000|sed "s/:.*$//g")
su $MAINUSER -c 'notify-send "starting some scan... "'
#....
su $MAINUSER -c 'notify-send "scan finished "'
which works fine and creates a notification, if you run it as root in a terminal, that was started in the desktop screen, but it doesn't if you change to another screen with Ctrl+Alt+F1 to start it from there as root.
I also tried it with export DISPLAY=:0; sudo -E -u rubo77 notify-send
, but that didn't change the behaviour either.
I would like to add it here in my script to run rkhunter regularely on a desktop system
relevant discussion: http://chat.stackexchange.com/rooms/17882/discussion-between-rubo77-and-muru
I also tried to use xuserrun
instead, but the same result
sudo -E
doesn't work, but it does – Alex Oct 16 '14 at 13:37-E
will export root's environment, not the user's sincesudo
is being run by root. Using-E
will only make this work if you run the script as the main user who started the X session. In that case, it works because the correctXAUTHORITY
variable is exported. – terdon Oct 16 '14 at 13:41sudo -E
was working. Also are you sureXAUTHORITY
is necessary? I use justxhost +
and withsu user -c
seems to work. – Alex Oct 16 '14 at 14:05-E
exports the environment of the user runningsudo
, if that was root, then it won't work. Yes,xhost +
is enough (as I point out in my answer) but it is not secure and exporting.Xauthority
is better. Withxhost +
any user can connect to the running X session. – terdon Oct 16 '14 at 14:36sudo -E
) for crontab tasks (which should also correctly set HOME varUSER=$(basename $HOME)
) the-E
is only to let DISPLAY live which otherwise would be discarded by sudo. Anyway good anwser, sorry I didn't ready it all before, good to know about XAUTHORITY I'll try. – Alex Oct 16 '14 at 15:08sudo -E
exports the environment of the user who launched it. Your crontab will be run as you, if you try it as a different user (which is what the OP is doing) it won't work. – terdon Oct 16 '14 at 15:37