帮我在 Ubuntu 上设置 cron 作业

发布于 2024-11-04 05:09:47 字数 1505 浏览 5 评论 0原文

你好 :) 我想要一款每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-11-11 05:09:47

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

有几种解决方案:

  • 此处描述了类似的问题。他们的解决方案是运行 Xvfb 或类似的程序以允许所有进程访问 X,即使它们实际上不显示任何内容。
  • 此处描述了一种不同的方法。看看,如果只是导出相关变量(您可以在 Python 中执行此操作,或者将它们包装在 CRON 启动的脚本中,并将它们放在调用 python 解释器之前)是否可以解决您的问题。请注意,该线程讨论了 dbus-launch,它是一个守护进程启动进程,但 dbus-send 在这里处于同一保护伞下。
  • 只需按照此处所述在脚本中设置 DISPLAY 变量即可。这应该足以让 DBUS 运行。

我认为第三种解决方案是最简单的,但现在你有不止一种了。

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:

  • A similar problem is described here. Their solution is to run Xvfb or similar to allow all processes access to X, even if they don't actually display anything.
  • A different approach is described here. See, if just exporting the relevant variables (you can do that in Python or wrap them in the script CRON launches and put them right before the call to python interpreter) solves your problem. Note, the thread discusses dbus-launch, which is a daemon start process, but dbus-send is under the same umbrella here.
  • Just set the DISPLAY variable in your script as described here. That should be sufficient for DBUS to run.

I think the 3rd solution is the easiest, but now you have more than one.

傻比既视感 2024-11-11 05:09:47

不要执行 sudo crontab,而只需执行 crontab -e 以使 crontab 作为您的用户配置文件运行并提供系统 python 的完整路径,您可以通过 which python 获取它。

Don't do a sudo crontab, but just do a crontab -e for the crontab to be run as your userprofile and provide the full path to your system python, you can get this by which python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文