这是我的 python 代码,我需要在 cron 作业上运行它:
#!/usr/bin/env python3
from pymongo import MongoClient
import datetime
import csv
def raw_db_counts():
client = MongoClient('host', 27017)
user_name = 'xxx'
password = 'ddd'
db = client.raw_data
db.authenticate(user_name, password, source='admin')
current_timestamp = datetime.datetime.now()
public_docs = db.public_docs.aggregate([
{'$group': {'_id': '$company_id'}},
{'$group': {'_id': 0, 'count': {'$sum': 1}}}
])
public_doc = next(public_docs)
public_docs_count = public_doc['count']
directorship_records = db.full_directorships.aggregate([
{'$group': {'_id': '$din_or_dpin'}},
{'$group': {'_id': 0, 'count': {'$sum': 1}}}
])
directorship_record = next(directorship_records)
directorship_records_count = directorship_record['count']
din_details_count = db.din_details.count()
with open('raw_db_count.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([current_timestamp, public_docs_count, directorship_records_count, din_details_count])
if __name__ == '__main__':
raw_db_counts()
在 shell 中我运行crontab -e
,然后编辑文件*/3 * * * * /home/fullpath/raw_db_counts.py
,但是当我运行 python 脚本 cron job 时,文件数据没有得到更新。
如果我正常执行 python 脚本则不会出现问题,但在 cronjob 执行中会出现问题。