linux - GtkWarning 无法打开显示 - 我如何从我的守护进程通知发送

标签 linux ubuntu python-2.7 gtk upstart

我有一个使用以下命令的 python-2.7.4 守护进程脚本:

from os import popen
cmd = 'notify-send --icon=ICON_FILE --urgency=critical  -t 10000 \'SCRIPT_NAME\' \'Here is my message\''
popen('su USER -c -p \"'+cmd+'\"')

此命令以 ROOT 身​​份在 python 命令 shell 中运行良好。但是,它在作为守护进程运行时不起作用。守护进程运行良好(在轮询时记录数据),但未按要求提供用户反馈。

守护进程使用 Upstart 和/etc/init/SCRIPT.conf 文件在 Mint (Ubuntu) 上运行。/var/log/upstart/SCRIPT.log 文件在重新启动后只有一个条目:

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/_init_.py:57: GtkWarning: 无法打开显示警告.warn( str(e), _gtk.警告)

我发现登录用户使用以下命令:

popen('users')

从我发现的其他情况来看,向其他用户显示发送消息存在安全问题,并且由于守护程序从 ROOT 运行,它返回上述 GtkWarning。如何让我的 python 守护进程通过 osd 和 notify-send 提供用户反馈?

最佳答案

GTKWarning不适用于发送notify-send的问题。当然,守护程序以 root 身份运行,需要使用“DISPLAY”变量来访问用户的显示。

export DISPLAY=:0

我通过使用“w”命令查找用户的显示并在发送通知发送命令之前更改此变量来解决此问题。守护进程现在可以正确地将通知发送到用户的 DISPLAY。

关于linux - GtkWarning 无法打开显示 - 我如何从我的守护进程通知发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20303068/

相关文章:

php - RuntimeException 未安装 Zip PHP 扩展

php - 我怎样才能提取 drupal-x.x.tar.gz 文件

python-2.7 - 当我同时拥有 python2 和 python3 时,如何通过 pip 安装 python2 软件包?

json - 将换行符 (\n) 替换为双反斜杠 n (\\n)

node.js - 无法在 ubuntu 16.04 中成功更新 npm

python - Google 和 Oauthlib - 范围已更改

python请求检查文件是否正确下载

linux - Freeradius 读取 google_authenticator secret 时出现问题 Centos 7

linux - 将 Linux 目录中最旧文件的日期(时间戳)存储在变量(shell 脚本)中

ruby - 如何在 sublime-text-2 中运行 ruby​​ 文件