【问题标题】:PyNotify not working from cron?PyNotify 不能从 cron 工作?
【发布时间】:2010-11-26 08:59:44
【问题描述】:

我编写了一个使用 pynotify 发出警报的脚本。当我运行它 (python script.py) 时它工作得很好,但是当使用 00 * * * * myname python ~/scripts/script.py 从 cron 运行时,它不起作用!我不知道为什么。这是sn-p:

        if os.path.isfile(os.path.expanduser('~/.thumbnails/normal')+'/'+thumbnail):
            n = pynotify.Notification(video_file[0], 'finished download', os.path.expanduser('~/.thumbnails')+'/'+thumbnail)
        else:
            n = pynotify.Notification(video_file[0], 'finished download', '/usr/share/icons/gnome/48x48/mimetypes/gnome-mime-application-x-shockwave-flash.png')
        print n
        n.show()

将输出定向到 ~/log.file 给出:<pynotify.Notification object at 0x16d4e60 (NotifyNotification at 0x13804e0)> 并且没有错误,所以我不太确定还能去哪里看。

【问题讨论】:

    标签: python cron pynotify


    【解决方案1】:

    我对 cron 工作没有那么深入,但我对 pynotify 有所了解。它使用 libnotify 和一些 DBUS 的东西,所以它在某处调用 DBUS 和 iirc,它还传递了应该显示通知的显示 id。

    现在,默认情况下,cron 不适用于 GUI 应用程序,您必须指定一个显示器供它们使用:

    00 * * * * myname env DISPLAY=:0 python ~/scripts/script.py
    

    这将使 cron 使用当前显示(桌面)。

    如果您在 Ubuntu 上运行,您可能会对这个页面感兴趣:
    https://help.ubuntu.com/community/CronHowto

    【讨论】:

    • @Ivo Wetzel 很好的解释,但它在 gnome ubuntu 中对我不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多