0

我有一个 bash 脚本,仅当我的笔记本电脑连接到网络时才使用 notify-OSD 打印通知。所以我把 bash 脚本放在 /etc/network/if-up.d

我将一些日志消息打印到文件中以确认脚本确实正在运行。但是通知的东西似乎不起作用。我刚刚将这一行添加到文件中

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

但是,当我像这样从终端显式运行该脚本(通过双击它或)时:

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

setproxy 是 bash 文件的名称,然后我看到通知工作正常。为什么会有这种行为?我怎样才能解决这个问题?我正在使用 ubuntu 12.04

/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'
4

3 回答 3

2

如果你想使用DISPLAY,你也必须设置XAUTHORITY,但这有点复杂(取决于用户名和随机字符串)试试这个:

#!/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'
于 2012-12-30T19:35:02.523 回答
1

我怀疑是环境问题。

所以在你的脚本中,输入:

#!/bin/bash -x

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

告诉我们发生了什么。

编辑:您的脚本应以:

#!/bin/bash

source ~/.bashrc || source /etc/profile
export DISPLAY=:0
# rest of the script there
于 2012-12-30T16:45:52.517 回答
0

如果您正在运行的程序需要 X,则它不能在没有 X 的情况下运行。或者以某种方式授权它连接到您的 X 会话,或者从您的 X 会话中运行它。

于 2012-12-30T18:35:27.903 回答