简体   繁体   English

从crontab运行python脚本

[英]Running a python script from crontab

I've got a python program which runs via crontab and that works perfectly. 我有一个python程序,它通过crontab运行,并且运行正常。 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 它从命令行运行,但是,将其作为crontab程序运行会导致它失败

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. 编辑我希望这个程序仍然从cron运行,并能够利用通知用户它的工作。 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. 编辑2我尝试过使用root的crontab和sudo -u esr python script.py但是这也失败了,默默无闻。

Edit 3 It is possible! 编辑3这是可能的! 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\" "
* * * * * 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\" "

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

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

*/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

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. 如果您希望这样做,您将必须剥离所有对PyGTK和会话总线的引用的脚本。

You're trying to use GUI (GTK+ library calls) for cron program that has no access to graphical terminal. 您正在尝试对无法访问图形终端的cron程序使用GUI(GTK +库调用)。 You need to avoid creating dialogs and windows when you run it from cron. 从cron运行时,您需要避免创建对话框和窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM