2011-04-28 82 views
0

嗨cron作业:)我想有一个每20分钟,通知一类应用,并决定通过开发一个自己使用Tomboy记事。我读了关于crontab并通过sudo crontab -e命令设置了一个工作。帮我安装在Ubuntu

*/20 * * * * python /home/phantom/Desktop/alarm.py 2>/home/phantom/Desktop/whatswrong.log

我的Python代码会是这样的:

#!/usr/bin/env python 
import dbus, gobject, dbus.glib 
# Get the D-Bus session bus 
bus = dbus.SessionBus() 
# Access the Tomboy D-Bus object 
obj = bus.get_object("org.gnome.Tomboy","/org/gnome/Tomboy/RemoteControl") 
# Access the Tomboy remote control interface 
tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl") 
# Display the Start Here note 
tomboy.DisplayNote(tomboy.FindNote("alert")) 

我不知道有关的DBus接口什么,但阅读使用dbus与假小子接口的教程,并想出了以上代码。

当我运行的代码手动我可以打开假小子说明(警报消息),但使用cron我碰到下面的错误,我无法理解。请帮我解决。谢谢:)

Traceback (most recent call last): 
File "/home/phantom/Desktop/try.py", line 4, in <module> 
bus = dbus.SessionBus() 
File "/usr/lib/pymodules/python2.6/dbus/_dbus.py", line 219, in __new__ 
mainloop=mainloop) 
File "/usr/lib/pymodules/python2.6/dbus/_dbus.py", line 108, in __new__ 
bus = BusConnection.__new__(subclass, bus_type, mainloop=mainloop) 
File "/usr/lib/pymodules/python2.6/dbus/bus.py", line 125, in __new__ 
bus = cls._new_for_bus(address_or_type, mainloop=mainloop) 
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.Spawn.ExecFailed: /bin/dbus- launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed. 
+0

如果我看到这个权利,你的cronjob将运行根和根没有一个X11会话。现在,如果我们应该迁移它,它应该在SU还是SF? – Bobby 2011-04-28 14:09:40

+0

@Bobby,我会说SU – dawebber 2011-04-28 15:40:35

回答

2

的根本问题是,一个运行的X会话在您需要的情况下,和cron脚本运行时,它运行没有这样一个会话(实际上它运行在所有的终端分离)。 Dbus可执行文件需要能够初始化X会话(它实际上并不需要运行X)。

有几个解决方案:

  • 类似的问题是described here。他们的解决方案是运行Xvfb或类似的操作来允许所有进程访问X,即使它们实际上没有显示任何东西。
  • 不同的方法是described here。看到,如果只是导出相关的变量(你可以用Python做到这一点,或者将它们包装在CRON启动的脚本中,并在调用python解释器之前将它们放入)解决了你的问题。请注意,该线程讨论了dbus-launch,这是一个守护进程启动进程,但dbus-send在这里处于同一个伞下。
  • 只需在脚本中将DISPLAY变量设置为described here即可。这对DBUS来说应该足够了。

我觉得第三个解决方案是最简单的,但现在你有一个以上的。

+0

我已经在ubuntu论坛了解展示解决方案,但没有尝试它,因为我没有弄懂:P。现在它设置后就可以工作了。感谢:d'*/20 * * * *出口DISPLAY =:0 &&蟒蛇/home/phantom/Desktop/alarm.py 2> /家/幻/桌面/ whatswrong.log' – sarath 2011-04-28 14:51:31

+0

BTW我能得到更多的了解X会话?任何资源? – sarath 2011-04-28 14:52:48

+0

@Sarath,这取决于你想知道的。因为它涉及DBUS或一般? – dawebber 2011-04-28 15:39:19

0

不做sudo crontab中,但只是做一个crontab -e的crontab来运行为您的用户配置文件,并提供完整的路径到你的系统的Python,您可以通过which python得到这个。

+0

我得到了同样的错误Senthil。并且“提供你的系统python的完整路径”的含义? – sarath 2011-04-28 14:22:52

+0

python可执行文件的完整路径,例如'在/ usr/bin中/ python'。 – samplebias 2011-04-28 14:31:22

+0

@samplebias @Senthil Kumaran你是否有这部分,但添加它在哪里? – sarath 2011-04-28 14:32:59