python-notify模块&cron:gio.Error [英] python-notify module & cron: gio.Error

查看:57
本文介绍了python-notify模块&cron:gio.Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求帮助,以使用 python-crontab 显示通知,因为一切我试过不工作.当cron启动脚本时,不会初始化显示.当我手动启动它时,就可以了.我尝试过的代码:

I'm asking some help to show notifications using python-crontab, because everything I've tried do not work. The display is not initilised when the script is launched by cron. When I start it manually, that's work. The codes I've tried:

    #!/usr/bin/env python
    # coding: utf8

    import subprocess
    import os

    #os.environ.setdefault("XAUTHORITY", "/home/guillaume" + "/.Xauthority")

    #os.environ.setdefault('DISPLAY', ':0.0')     # do not work
    #os.environ['DISPLAY'] = ':0.0'               # do not work
    print = os.environ

    cmd2 = 'notify-send test'
    subprocess.call(cmd2, shell=True)

    # more code, which is working (using VLC)
    cmd3 = "cvlc rtp://232.0.2.183:8200 --sout file/mkv:/path/save/file.mkv" # to download TV's flow
    with open("/path/debug_cvlc.log", 'w') as out:
        proc = subprocess.Popen(cmd3, stderr=out, shell=True, preexec_fn=os.setsid)
    pid = proc.pid                  # to get the pid
    with open("/path/pid.log", "w") as f:
       f.write(str(pid))            # to write the pid in a file
    # I'm using the pid to stop the download with another cron's task, and to display another notify message. 
    # Download and stop is working very well, and zenity too. But not notify-send

谢谢

{'LANG': 'fr_FR.UTF-8', 'SHELL': '/bin/sh', 'PWD': '/home/guillaume', 'LOGNAME': 'guillaume', 'PATH': '/usr/bin:/bin', 'HOME': '/home/guillaume', 'DISPLAY': ':0.0'}

Edit2:我在cron中这样调用脚本:

45 9 30 6 * export DISPLAY=:0.0 && python /home/path/script.py > /home/path/debug_cron_on.log 2>&1

我精确地说我有两个屏幕,所以我认为DISPLAY:0.0是显示此通知的方式.但我看不到.

I precise I have two screens, so I think DISPLAY:0.0 is the way to display this notify.. But I don't see it.

subprocess.call("zenity --warning --timeout 5 --text='this test is working'", shell=True)

我有通知发送版本0.7.3,我确定通知发送正在与终端一起使用.

I have notify-send version 0.7.3, and I precise that notify-send is working with the terminal.

import pynotify
pynotify.init("Basic")
n = pynotify.Notification("Title", "TEST")
n.show()

日志文件显示此:(法语)

The log file show this: (in french)

    Traceback (most recent call last):
      File "/home/path/script.py", line 22, in <module>
        n.show()
    gio.Error: Impossible de se connecter : Connexion refusée 
 #Translating: Unable to connect : Connection refused

那么,我对dbus有问题吗?这是什么?

So, I have problem with dbus? what is this?

cron = CronTab()
dbus = os.getenv("DBUS_SESSION_BUS_ADDRESS")   # get the dbus
# creating cron  
cmd_start = "export DBUS_SESSION_BUS_ADDRESS=" + str(dbus) + " && export DISPLAY=:0.0 && cd /path && python /path/script.py > path/debug_cron.log 2>&1"
job = cron.new(cmd_start)
job = job_start.day.on(self.day_on) # and all the lines to set cron, with hours etc..
cron.write()             # write the cron's file

最后,cron的代码如下:

Finally, the cron's line is like that:

20 15 1 7 * export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-M0JCXXbuhC && export DISPLAY=:0.0 && python script.py

然后显示通知.问题解决了!:)

Then the notification is displaying. Problem resolved !! :)

推荐答案

您像

45 9 30 6 * DISPLAY=:0.0 python /home/path/script.py > /home/path/debug_cron_on.log 2>&1

这是不正确的,因为您没有导出 DISPLAY 变量,并且随后的命令无法运行.

which is incorrect, since you are not exporting the DISPLAY variable, and the subsequent command does not run.

试试看

45 9 30 6 * export DISPLAY=:0.0 && cd /home/path/ && python script.py >> debug_cron.log 2>&1

此外,您还要在cron作业中设置 DISPLAY 变量,因此请尝试cron作业是否工作而不将其导出到作业行中

Also, you are setting the DISPLAY variable within your cron job as well, so try if the cron job works without exporting it in the job line

45 9 30 6 * cd /home/path/ && python script.py >> debug_cron.log 2>&1

编辑

在调试时,每分钟运行一次cron作业.以下对我有用:

While debugging, run the cron job every minute. Following worked for me:

Cron条目

* * * * *  cd /home/user/Desktop/test/send-notify && python script.py

script.py

script.py

#!/usr/bin/env python

import subprocess
import os

os.environ.setdefault('DISPLAY', ':0.0')
print os.environ

cmd2 = 'notify-send test'
subprocess.call(cmd2, shell=True)

编辑2

使用 pynotify ,script.py变为

Using pynotify, script.py becomes

#!/usr/bin/env python

import pynotify
import os

os.environ.setdefault('DISPLAY', ':0.0')

pynotify.init("Basic")
n = pynotify.Notification("Title", "TEST123")
n.show()

cron条目变为

* * * * *  cd /home/user/Desktop/test/send-notify && python script.py

编辑3

cron环境中缺少一个环境变量 DBUS_SESSION_BUS_ADDRESS .可以在方式

One environment variable DBUS_SESSION_BUS_ADDRESS is missing from the cron environment. It can be set in this and this fashion

这篇关于python-notify模块&amp;cron:gio.Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆