我有一个由cron执行的python脚本,它的目的是管理从用户到其他用户的消息。这些消息存储在mongodb数据库中。脚本在消息中爬行,查找目标名称, 从数据库获取它的\u id,并将消息数据存储在users消息数组中。之后,邮件将从邮件集合中删除。你知道吗
如果手动执行python脚本,一切正常,但是如果Cron运行它,用户将不会被更新,但是消息将被删除。你知道吗
mongodb服务器是2.4.10,我知道它很旧,但它是最新的版本,运行在raspberry pi 2上,afaik。 python版本是2.7.x
# ...
# find all messages in messages collection
cursorMsg = db.messages.find({})
# iterate over every key in cursor
for keyMsg in cursorMsg:
body = keyMsg["body"]
about = keyMsg["about"]
created_at = keyMsg["created_at"]
sender_id = keyMsg["sender_id"]
cursorUsrSender = db.users.find({"_id": str(sender_id)})
sender_name = keyMsg["sender_name"]
sender_id = keyMsg["sender_id"]
to = keyMsg["to"]
_id = str(keyMsg["_id"])
# find the user for the message
cursorUsrTarget = db.users.find({"username": to})
for keyUsrTarget in cursorUsrTarget:
print(keyUsrTarget)
usr_target_id = str(keyUsrTarget["_id"])
print(type(keyUsrTarget["messages"]))
new_message = {
"_id": _id,
"created_at": created_at,
"about": about,
"body": body,
"sender_id": sender_id,
"sender_name": sender_name,
"target_id": usr_target_id
}
# save the message
keyUsrTarget["messages"].append(new_message)
db.users.save(keyUsrTarget)
# delete the message from message collection
db.messages.remove({"_id": keyMsg["_id"]})
有没有一种方法可以等待save命令的响应,或者有没有其他方法可以在成功保存后执行delete命令?你知道吗
转储:
{u'username': u'test', u'hash': u'$2a$10$Irwx.S5gwpOOB/gAxHPAv.Fpge9i6H.mEIh.RrAwfLp.qboZwm2sq', u'firstName': u'test', u'lastName': u'test', u'schiffe': [{u'kriegsschiffe': {u'galleone': u'0', u'karacken': u'0'}}, {u'handelsschiffe': {u'koggen': u'0', u'schoner': u'0'}}], u'messages': [], u'fresh_account': u'false', u'test': u'0', u'islands': [{u'buildings': {u'resource_stores': [{u'capacity': u'1', u'level': u'1', u'max_capacity': u'1000', u'attack': u'100', u'health': u'100', u'type': u'Holzspeicher'}, {u'capacity': u'1', u'level': u'1', u'max_capacity': u'1000', u'attack': u'100', u'health': u'100', u'type': u'Steinspeicher'}, {u'capacity': u'1', u'level': u'1', u'max_capacity': u'1000', u'attack': u'100', u'health': u'100', u'type': u'Eisenspeicher'}, {u'capacity': u'1', u'level': u'1', u'max_capacity': u'1000', u'attack': u'100', u'health': u'100', u'type': u'Nahrungsspeicher'}], u'main_buildings': [{u'type': u'Hauptgeb\xe4ude', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'S\xe4gewerk', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Steinbruch', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Schmelzofen', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'M\xfchle', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Hafen', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Forschungsgeb\xe4ude', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Handelsdepot', u'attack': u'100', u'health': u'100', u'level': u'1'}, {u'type': u'Fort', u'attack': u'100', u'health': u'100', u'level': u'1'}]}, u'island_name': u'Insel 19', u'coordinates': {u'y': 450, u'x': 250}, u'ocean': 0, u'shape': 67, u'owner': u'test', u'_id': u'57246661e844a270258159f1'}], u'_id': ObjectId('57283a079d3a22c819ca8600')} Message send to user. Message deleted from collection.
克朗塔布
*/1 * * * * pi ( python /home/py/menage_messages.py >> /home/log/messages.log )
与手动执行脚本的环境相比,Cron作业在不同的环境中运行。正如上面的注释所建议的,您可以尝试将stdout和stderr重定向到一个文件,然后查看作为cron作业运行脚本的情况。你知道吗
相关问题 更多 >
编程相关推荐