从crontab运行python脚本 [英] Running a python script from crontab

查看:164
本文介绍了从crontab运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在做什么错了?

编辑
我希望该程序仍可以从cron运行,并能够利用向用户通知它的工作情况。有什么方法吗?

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?

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

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

编辑3
可能!这是代码。

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\" "


推荐答案

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

这篇关于从crontab运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆