帮我在 Ubuntu 上设置 cron 作业
你好 :) 我想要一款每 20 分钟通知一次的应用程序,并决定使用 Tomboy 笔记自己开发一个。我阅读了有关 crontab 的信息,并通过 sudo crontab -e 命令设置了一个作业。
*/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 与 Tomboy 接口的教程并得出上述代码。
当我手动运行代码时,我可以打开 Tomboy 注释(警报消息),但使用 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.
Hi :) I wanted to have a every-20 mins-notifier kind of app and decided to develop one by myself using Tomboy notes. I read up about crontab and set a job through the sudo crontab -e
command.
*/20 * * * * python /home/phantom/Desktop/alarm.py 2>/home/phantom/Desktop/whatswrong.log
And my python code would go like:
#!/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"))
I don't know anything about the DBus interface but read a tutorial that uses DBus to interface with Tomboy and came up with the above code.
When I run the code manually I could open the Tomboy note(alert message) but with the cron I get the following error which I couldn't understand. Please me help me out. Thanks :)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根本问题是,您的情况需要一个正在运行的 X 会话,并且当 CRON 脚本运行时,它在没有这样的会话的情况下运行(它实际上与任何终端分离运行)。 Dbus 可执行文件需要能够初始化 X 会话(它实际上不需要正在运行的 X)。
有几种解决方案:
我认为第三种解决方案是最简单的,但现在你有不止一种了。
The fundamental issue is that an running X session is required in your case, and when CRON script runs, it runs without such a session (it actually runs detached from any terminal). Dbus executable needs to be able to initialize X session (it doesn't actually need a running X).
There are a couple of solutions:
I think the 3rd solution is the easiest, but now you have more than one.
不要执行 sudo crontab,而只需执行 crontab -e 以使 crontab 作为您的用户配置文件运行并提供系统 python 的完整路径,您可以通过
which python
获取它。Don't do a
sudo
crontab, but just do acrontab -e
for the crontab to be run as your userprofile and provide the full path to your system python, you can get this bywhich python
.