2

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

rubo77
  • 28,966
  • Maybe this is a hint how it could work: http://ubuntuforums.org/showthread.php?t=775170&p=6087197#post6087197#2 although it didn't plain like posted there and I don't hae the knowledge about DBUS – rubo77 Oct 16 '14 at 13:16
  • I use it with $USER which is set by the system – Alex Oct 16 '14 at 13:22
  • I'm sorry, did you deleted comments? Also your edit now show that sudo -E doesn't work, but it does – Alex Oct 16 '14 at 13:37
  • @Alex no it doesn't. The OP is running this from a separate session (a tty) where they have logged in as root. The -E will export root's environment, not the user's since sudo 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 correct XAUTHORITY variable is exported. – terdon Oct 16 '14 at 13:41
  • @terdon there was a comment by @rubo77 where he wrote sudo -E was working. Also are you sure XAUTHORITY is necessary? I use just xhost + and with su user -c seems to work. – Alex Oct 16 '14 at 14:05
  • @Alex I deleted the comments since the information had been added to the question. The deleted comment was "yes export DISPLAY=:0; sudo -E -u rubo77 notify-send test works, but still only If I execute the script directly, not from cron". Again, that will only work if run as the user who started the X session, -E exports the environment of the user running sudo, 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. With xhost + any user can connect to the running X session. – terdon Oct 16 '14 at 14:36
  • @terdon, I was using it (sudo -E) for crontab tasks (which should also correctly set HOME var USER=$(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:08
  • @Alex yes but that's because you were using it in your crontab and you were the owner of the X session. As I said, sudo -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
  • @Alex while the points I made were true, they seem to be irrelevant here because inotify-send works via D-bus and is not affected by the XAUTHORITY/xhosts stuff. Apparently. – terdon Oct 16 '14 at 16:56
  • The marked duplicate doesnt have such a nice answer like this answer: http://unix.stackexchange.com/questions/111188/using-notify-send-with-cron – rubo77 Oct 17 '14 at 18:18
  • Please reopen this so I can post parts of the answer here: http://askubuntu.com/a/537721/34298 – rubo77 Oct 18 '14 at 07:08

0 Answers0