使用 cron 运行 pymongo 脚本的问题
我正在运行一个简单的 python 脚本,将数据发送到 mongodb
#!/usr/bin/env python
import sys
import time
from datetime import datetime
import pymongo
from pymongo import Connection
today = { 'date and time' : datetime.today() }
connection = Connection()
db = connection.tests
collection = db.times
collection.insert(today)
我正在尝试使用 cron 来每分钟安排一次。我已经使用 crontab 来设置它,
* * * * * /Users/MyUser/XX/YY/ZZ/timetest.py
并且我可以使用正确目录中的 python timetest.py
完美执行它;但是该程序仍然无法自行运行。我觉得我已经非常接近让它工作了,有人可以帮助我吗?
I'm running a simple python script sending data to a mongodb
#!/usr/bin/env python
import sys
import time
from datetime import datetime
import pymongo
from pymongo import Connection
today = { 'date and time' : datetime.today() }
connection = Connection()
db = connection.tests
collection = db.times
collection.insert(today)
And I'm trying to use cron to schedule this every minute. I've used crontab to set this
* * * * * /Users/MyUser/XX/YY/ZZ/timetest.py
And I can execute this perfectly using python timetest.py
from the correct directory; however the program is still not running on its own. I feel like I'm very close to getting it to work, can anyone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cron 环境可能与您的用户环境不匹配。在 cron 中,您可以像这样在 crontab 中设置路径变量
,或者您可以在脚本上显式调用 python 二进制文件
,或者您可以在脚本中设置 shebang 行以显式引用 python 二进制文件(如果您曾经使用过,这可能是不可取的虚拟环境)
It is likely that the cron environment does not match your user's environment. In cron you can either set the path variable in crontab like
or you can just explicitly call the python binary on your script
or you can set the shebang line in your script to reference the python binary explicitly (this may not be desirable if you ever use virtualenv)