从 crontab 运行 python 脚本
我有一个通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据建议, 一个解释,不幸的是不是我的
As per a suggestion, an explanation, unfortunately not mine
您尝试在资源不可用的环境中运行需要用户资源的脚本。如果您希望它起作用,您必须删除脚本中对 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.
我只是想提一下,以下配方适用于出色的窗口管理器的用户:
I just wanted to mention that the following recipe works for users of the awesome window manager:
您正在尝试对无法访问图形终端的 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.