从 crontab 运行 python 脚本

发布于 2024-09-10 16:17:31 字数 819 浏览 7 评论 0原文

我有一个通过 crontab 运行的 python 程序,并且运行良好。然而,我决定添加通知我它正在做什么的功能,但它突然失败了。它从命令行运行,但是,将其作为 crontab 程序运行会导致失败

libnotify-Message: Unable to get session bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

我做错了什么?

编辑 我希望这个程序仍然从 cron 运行,并且能够利用通知用户它的工作。有什么办法可以做到这一点吗?

编辑2 我尝试过使用 root 的 crontab 和 sudo -u esr python script.py 但这也失败了,默默地。

编辑3 这是可能的!这是代码。

* * * * * su $user -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "

I've got a python program which runs via crontab and that works perfectly. However, I decided to add the ability to notify me of what it's doing, and suddenly it's failing. It runs from the command line, however, running it as a crontab program causes it to fail

libnotify-Message: Unable to get session bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

What am I doing wrong?

Edit
I would like this program to still run from cron and be able to take advantage of notifying the user of it's work. Is there any way to do this?

Edit 2
I've tried using root's crontab and sudo -u esr python script.py yet this also fails, silently at that.

Edit 3
It is possible! Here's the code.

* * * * * su $user -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

可是我不能没有你 2024-09-17 16:17:31
* * * * * su esr -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "

根据建议, 一个解释,不幸的是不是我的

* * * * * su esr -c "DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(ps -au esr | grep -i "gnome-session" | awk '{ print $1 }')/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') $(whereis notify-send | awk '{ print $2 }') -u normal -t 20000 \"Hello\" "

As per a suggestion, an explanation, unfortunately not mine

梦晓ヶ微光ヅ倾城 2024-09-17 16:17:31

您尝试在资源不可用的环境中运行需要用户资源的脚本。如果您希望它起作用,您必须删除脚本中对 PyGTK 和会话总线的所有引用。

You're trying to run a script that requires user resources in an environment where said resources are not available. You will have to strip the script of all references to PyGTK and to the session bus if you want this to work.

亢潮 2024-09-17 16:17:31

我只是想提一下,以下配方适用于出色的窗口管理器的用户:

*/1 * * * * DBUS_SESSION_BUS_ADDRESS=$(grep -zi DBUS /proc/$(pgrep awesome)/environ | sed -r -e 's/^DBUS_SESSION_BUS_ADDRESS=//') DISPLAY=":0.0" notify-send -t 0 blah blah

I just wanted to mention that the following recipe works for users of the awesome window manager:

*/1 * * * * DBUS_SESSION_BUS_ADDRESS=$(grep -zi DBUS /proc/$(pgrep awesome)/environ | sed -r -e 's/^DBUS_SESSION_BUS_ADDRESS=//') DISPLAY=":0.0" notify-send -t 0 blah blah
就像说晚安 2024-09-17 16:17:31

您正在尝试对无法访问图形终端的 cron 程序使用 GUI(GTK+ 库调用)。当您从 cron 运行它时,您需要避免创建对话框和窗口。

You're trying to use GUI (GTK+ library calls) for cron program that has no access to graphical terminal. You need to avoid creating dialogs and windows when you run it from cron.

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