Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本在自动运行时工作不正常_Bash_Networking_Ubuntu_Notifications_System - Fatal编程技术网

Bash脚本在自动运行时工作不正常

Bash脚本在自动运行时工作不正常,bash,networking,ubuntu,notifications,system,Bash,Networking,Ubuntu,Notifications,System,我有一个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' 但是,当我从终端显式运行

我有一个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'

我怀疑是环境问题

因此,在脚本中,输入:

#!/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

我怀疑是环境问题

因此,在脚本中,输入:

#!/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

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

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

如果您想使用
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'

如果要使用
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'

谢谢你的回复。我想说清楚,你的意思是我需要添加这样的内容:#!source~/.bashrc | |/etc/profile而不是通常的#/bin/bash?@VivekPradhan,不,
source
应该在
后面的下一行/bin/bash
Ok@sputnick我试过这个:#!bin/bash source~/.bashrc | source/etc/profile,但我仍然没有看到通知。您可以尝试将一个简单的bash脚本放入/etc/network/if up.d中,只显示一个通知,然后让我知道它是如何工作的。有关调试模式,请参阅我编辑的文章。然后用/tmp/trace文件的输出编辑您的原始帖子。非常感谢@sputnick!我已经用/tmp/tracethanks的输出更新了我的问题,以供您答复。我想说清楚,你的意思是我需要添加这样的内容:#!source~/.bashrc | |/etc/profile而不是通常的#/bin/bash?@VivekPradhan,不,
source
应该在
后面的下一行/bin/bash
Ok@sputnick我试过这个:#!bin/bash source~/.bashrc | source/etc/profile,但我仍然没有看到通知。您可以尝试将一个简单的bash脚本放入/etc/network/if up.d中,只显示一个通知,然后让我知道它是如何工作的。有关调试模式,请参阅我编辑的文章。然后用/tmp/trace文件的输出编辑您的原始帖子。非常感谢@sputnick!我已经用/tmp/traceThanks a lot@triplee的输出更新了我的问题,以供您回复。我想你是对的,但我不明白为什么它需要X授权?我将尝试了解如何导出Xsession变量以进行授权。非常感谢@triplee的回复。我想你是对的,但我不明白为什么它需要X授权?我将尝试并了解如何导出Xsession变量以进行授权。