Cron execution of python script not working like expected <p>I have a python script which is executed by cron, it's purpose is to manage messages from users to other users. These messages are stored inside a mongodb database. The script crawls through the messages, looks for the target name, fetches it's _id from the db and stores the message data inside the users message array. After that the message is deleted from the message collection.</p> <p>If I execute the python script manuallly everything works fine, but if Cron runs it the user will not be updated but the message will be deleted.</p> <p>The mongodb server is 2.4.10, I know it's old but it's the latest verion which runs on a raspberry pi 2, afaik. The python version is 2.7.x</p> <pre><code># ... # 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"]}) </code></pre> <p>Is there a way to wait for the response of the save command or any other way to execute the delete command after a successful save?</p> <p>Dump:</p> <blockquote> <p>{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.</p> </blockquote> <p>crontab</p> <pre><code>*/1 * * * * pi ( python /home/py/menage_messages.py &gt;&gt; /home/log/messages.log ) </code></pre>