作为 cron 作业的通知
我编写了一个 python 应用程序,它作为 cron 作业运行正常。 正在发生的情况的消息通知,但 cron 似乎无法运行这样的应用程序。
后来我添加了一些库(例如 pynotify
和其他 *),因为我想收到描述 您知道如何每五分钟运行此应用程序一些替代方法吗?我用的是Xubuntu。
import gtk, pygtk, os, os.path, pynotify
我可以在没有 cron 的情况下运行应用程序,没有任何问题。
Cron 似乎运行应用程序,但不会显示通知消息。在 /var/log/cron.log
中没有错误。该应用程序每分钟执行一次,没有出现任何问题。
我的计划表: */1 * * * * /home/xralf/pythonsrc/app
谢谢
I coded a python application which was running OK as a cron job. Later I added some libraries (e.g. pynotify
and other *) because I wanted to be notified with the message describing what is happening, but it seems that cron can't run such an application.
Do you know some alternative how to run this application every five minutes? I'm using Xubuntu.
import gtk, pygtk, os, os.path, pynotify
I can run the application without cron without problems.
Cron seems to run the application but it won't show the notification message. In /var/log/cron.log
there are no errors. The application executed every minute without problems.
my crontab:*/1 * * * * /home/xralf/pythonsrc/app
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 python 脚本本身运行良好并且仅在
cron
中失败,则很可能没有在 cron 中设置库的路径。这是我的 cronjobs 之一的示例,其中我在执行文件之前添加了 cron 的路径。您必须将上面的路径替换为您的库的相应路径。
If your python script runs fine by itself and only fails in
cron
, then most likely the paths to the libraries are not set in cron. Here's an example from one of my cronjobs where I add the path to cron before executing the fileYou'll have to replace the paths above with the respective paths to your libraries.
如果 cron 作业以“您”的身份运行,并且您设置了 DISPLAY var (export DISPLAY=:0),那么您应该不会有任何问题。
If the cron job runs as "you", and if you set the DISPLAY var (export DISPLAY=:0) you should have no issues.
我在 pynotify 的 cron 作业中没有看到任何问题?您遇到的错误是什么?
您可以单独运行您的 python 代码来检查您的 python 代码是否运行良好但仅因 cron 而失败吗?
Celery是分布式作业队列&用 Python 编写的任务管理器,但它可能无法满足您的需求。
如果您知道程序将在 5 分钟内关闭,Supervisord 还可以执行某种 cron 任务。因此,您可以配置supervisord以使其稍后启动任务。它们都不像 cron job 那样容易。
I don't see any problem in cron job with pynotify? What is the error you are getting?
Can you run your python code separately to check whether your python code is working really well but only fails with cron?
Celery is distributed job queue & task manager written in Python but it may be too much for your needs.
Supervisord also can do some sort of cron task if you know that your program shall close in 5 minutes. So you can configure supervisord to start the task soon after. None of them are not easier like cron job.
您最可能的问题是路径问题。 Cron 没有用户路径,除非您专门设置它。因此,如果您的库安装在非标准位置或用户帐户中,则 cron 将需要完整路径,否则您需要在 crontab 文件中设置路径变量。
将该行添加
到您的 crontab 文件中并查看它是否有效。
Your most likely problem is a pathing issue. Cron does not have user paths unless you specifically set it up. So if your libraries are installed in non standard locations or user accounts, cron will need full paths or else you need to set the path variable in your crontab file.
Add the line
to your crontab file and see if it works.