0

I have a bash script that prints a notification using the notify-OSD only when my laptop is connected to a network. So I have placed the bash script in /etc/network/if-up.d

I printed some log messages into a file to confirm that the script is indeed running. However the notification thing does not seem to work. I just added this line to the file

 notify-send -u 'critical' -i /home/vivek/Downloads/proxy.ico 'SetProxy Status' 'proxy set to auto'

However when i run that script explicitly (by double clicking on it or) from the terminal like this:

cd /etc/network/if-up.d
./setproxy

setproxy is the name of the bash file, then i see the notification working perfectly. Why this behaviour? How can i fix this? I am using ubuntu 12.04

Output in /tmp/trace :

+ nmcli con status
+ grep -q 'Hostel\|IITD'
+ '[' 1 -eq 0 ']'
+ gsettings set org.gnome.system.proxy mode none

** (process:12320): WARNING **: Command line `dbus-launch     --autolaunch=673e71ca3fc5f402403d22380000000a --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n

** (process:12320): WARNING **: Command line `dbus-launch --autolaunch=673e71ca3fc5f402403d22380000000a --binary-syntax --close-stderr' exited with   non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
+ echo 'You are outside college! No Proxy'
+ notify-send -u critical -i /home/vivek/Downloads/proxy.ico 'SetProxy Status' 'You are outside college, proxy set to none'
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46

3 Answers3

2

If you want to use DISPLAY, you have to set XAUTHORITY too, but that's a little more complicated (depending on username and a random string) try this:

#!/bin/bash

export displayOwner=vivek     # enter your usename here
export DISPLAY=:0
export XAUTHORITY=$(echo /var/run/gdm3/auth-for-${displayOwner}-*/database)

notify-send -u 'critical' -i /home/vivek/Downloads/proxy.ico 'SetProxy Status' 'proxy set to auto'
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
1

I suspect an environment problem.

So in your script, put :

#!/bin/bash -x

source ~/.bashrc || source /etc/profile
exec &>/tmp/trace
# rest of the script there

and tell us what's going on.

Edit : Your script should start with :

#!/bin/bash

source ~/.bashrc || source /etc/profile
export DISPLAY=:0
# rest of the script there
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • thanks for your reply. Just to be clear, Do you mean i need to add something like this: #! source ~/.bashrc || /etc/profile instead of the usual #!/bin/bash ? – Vivek Pradhan Dec 30 '12 at 16:56
  • 1
    @VivekPradhan, no, `source` should be on the *next* line after the `#!/bin/bash` – glenn jackman Dec 30 '12 at 17:15
  • Ok @sputnick i tried this : #!bin/bash source ~/.bashrc || source /etc/profile , but i still do not see the notification. You can try and put a trivial bash script that just shows a notification and put it in /etc/network/if-up.d and let me know how you got it working. – Vivek Pradhan Dec 30 '12 at 17:16
  • See my edited post for debug mode. Then edit your original post with the output from /tmp/trace file – Gilles Quénot Dec 30 '12 at 17:18
  • Thanks a lot @sputnick! I have updated my question with the output from /tmp/trace – Vivek Pradhan Dec 30 '12 at 18:06
  • Thanks a lot for your help @sputnick , adding "export DISPLAY=:0 && export XAUTHORITY=/home/vivek/.Xauthority " to my script fixed the error with notifications. I just exported Display without exporting Xauthority and i was still getting some Xauthorisation error, http://cweiske.de/tagebuch/DBus%20notify-send%20over%20network.htm really explains this problem. – Vivek Pradhan Jan 02 '13 at 17:56
0

If the program you are running requires X, it cannot run without X. Either somehow authorize it to connect to your X session, or run it from within your X session.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks a lot @triplee for your reply. I guess you are right, but i don't understand why would it need Xauthorisation? I will try and found out how can i export Xsession variables for authorization. – Vivek Pradhan Dec 30 '12 at 18:56