私はcronによって実行されるpythonスクリプトを持っています。それは、ユーザーから他のユーザーにメッセージを管理することです。これらのメッセージは、mongodbデータベース内に格納されます。スクリプトはメッセージをクロールし、ターゲット名を探します。 はdbから_idを取得し、メッセージのデータをユーザーのメッセージ配列に格納します。その後、メッセージはメッセージコレクションから削除されます。PythonスクリプトのCron実行が期待通りに機能しない
私はpythonスクリプトをmanualllyすべてうまく動作しますが、Cronを実行すると、ユーザーは更新されませんが、メッセージは削除されます。
mongodbサーバーは2.4.10ですが、私はそれが古いと知っていますが、ラズベリーのパイ2、afaikで動作する最新のバージョンです。 Pythonのバージョンは2.7.35
# ...
# 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コマンドの応答や保存に成功した後に削除コマンドを実行するための他の方法を待つ方法はありますか?
ダンプ:
{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'grione':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'is' ':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'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 'タイプ':u'S \ xe4gewerk '、u'attack':u'100 '、u'health':u'100 '、u'level':u'1 '}、{u'type':u'Steinbruch ' u'attack ':u'attack':u'attack ':u'attack':u'attack ':u'attack':u'Schmelzofen: 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 'レベル':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'island_name':u'Insel 19 ':' u'Fort '、u'attack':u'100 '、u'health':u'100 '、u'level':u'1 ' 、u'coordinates ':{ 450、u'x ':250}、u'ocean':0、u'shape ':67、u'owner':u'test '、u'_id':u'57246661e844a270258159f1 '}] 、u'_id ':ObjectId(' 57283a079d3a22c819ca8600 ')} メッセージをユーザーに送信します。 コレクションからメッセージが削除されました。手動でスクリプトを実行する環境と比較して
のcrontab
*/1 * * * * pi (python /home/py/menage_messages.py >> /home/log/messages.log)
競合状態が発生する可能性はありますか?しかし、私が理解していないことは、手動で起動するとスクリプトがうまく動作することです。この問題は、cronがスクリプトを実行するときに発生します。 – ronald
は、cronの結果をテキストファイルに記録し、whats wrongeを見ることができます。 '* * * * */full/path/of/your/scriptです。py> text_dump'、さらに、フルパスを持っていることを確認してください –
こんにちは@ http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work –