【问题标题】:Python code working in a terminal but not in a cronjobPython 代码在终端中工作,但不在 cronjob 中
【发布时间】:2014-10-14 15:37:10
【问题描述】:

我编写了一个代码,它会在执行时通知我。我想每半小时运行一次这段代码,因此创建了一个 cronjob。但是通过cronjob执行时代码不起作用。

这是我的代码:

import sys
import pynotify

if __name__ == "__main__":
    if not pynotify.init("icon-summary-body"):
        sys.exit(1)
    n = pynotify.Notification("Subject","Message","notification-message-im")
    n.show() #throws error here

定时任务:

* * * * * cd /home/username/Documents && /usr/bin/python file.py >> /home/username/Desktop/mylog.log 2>&1

Cronjob 日志:

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
  warnings.warn(str(e), _gtk.Warning)
Traceback (most recent call last):
  File "file.py", line 8, in <module>
    n.show()
gio.Error: Cannot autolaunch D-Bus without X11 $DISPLAY

可能的原因是什么?

我也尝试使用notify2,但没有成功。正常执行但不能通过 cronjob 工作

这是 notify2 代码:

import notify2
notify2.init('app name')
n = notify2.Notification("Summary","Some body text","notification-message-im")
n.show()

notify2 脚本的 Cronjob 日志:

Traceback (most recent call last):
  File "file.py", line 3, in <module>
    notify2.init('app name')
  File "/usr/local/lib/python2.7/dist-packages/notify2.py", line 93, in init
    bus = dbus.SessionBus(mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 211, in __new__
    mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/_dbus.py", line 100, in __new__
    bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop)
  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 122, in __new__
    bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

如何通过 cronjob 运行此代码?

编辑

使用以下代码:-

import sys
import pynotify
import os

os.environ.setdefault('XAUTHORITY', '/home/user/.Xauthority')
os.environ.setdefault('DISPLAY', ':0.0')

if __name__ == "__main__":
    if not pynotify.init("icon-summary-body"):
        sys.exit(1)
    n = pynotify.Notification("Subject","Message","notification-message-im")
    n.show() #throws error here

我现在得到:-

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
  warnings.warn(str(e), _gtk.Warning)
Traceback (most recent call last):
  File "file.py", line 12, in <module>
    n.show() #throws error here
gio.Error: Could not connect: Connection refused

【问题讨论】:

    标签: python cron pynotify


    【解决方案1】:

    您是否尝试在您的 cronjob 中调用显示?

    定时任务:

    * * * * * DISPLAY=:0.0 python /home/username/Documents/file.py
    

    在您的python代码中,您也可以尝试在开头调用Display:

    import os
    # environnement vars
    os.environ.setdefault('XAUTHORITY', '/home/user/.Xauthority')
    os.environ.setdefault('DISPLAY', ':0.0')
    

    另外,pynotify 不能在 root 中工作。所以你应该在没有“sudo”的情况下编写你的 crontab

    crontab -e
    

    【讨论】:

    • 你也可以尝试在你的python代码中调用显示,我已经编辑了我的答案。
    • 你是在 root 中调用 pynotify 还是作为普通用户调用? -> 如何修改你的任务:contab -esudo crontab -e
    • 我以普通用户的身份称呼它。我在没有sudo 的情况下修改了我的 crontab
    • 使用 sudo crontab -e 对我有用。你能解释一下为什么会这样吗?
    • 好的。因此,如果您使用 sudo 修改您的 crontab,该脚本将以 root 身份执行。并且 pynotify 在 root 中存在问题。所以,您应该尝试删除您的 sudo crontab,并使用 contab -e 重新调用您的脚本,而不使用 sudo
    【解决方案2】:

    尝试设置环境变量$DISPLAY

    * * * * * env DISPLAY=:0 cd /home/username/Documents && /usr/bin/python file.py >> /home/username/Desktop/mylog.log 2>&1
    

    【讨论】:

      猜你喜欢
      • 2015-09-29
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      相关资源
      最近更新 更多