【问题标题】:cronjob/task scheduler in djangodjango 中的 cronjob/任务调度程序
【发布时间】:2016-08-12 15:32:02
【问题描述】:

我是 cronjob 的新手。我想检索目录中的数据列表并将信息保存到我的模型中。

这是我的代码:

from os.path import getsize
import time
current_user = request.user

root = "/mnt/data/dashboard/"
for path, subdirs, files in os.walk(root):
    for name in files:
        file_name =  name
        size =  getsize(root)
        filepath = path 
        datastr = os.path.join(filepath,name)
        files = datastr.replace( "/mnt/data/dashboard/","" )
        date =  time.ctime(os.path.getctime(root))

        # insert = File(file=files, owner=current_user, _file_size=size, uploaded_at=date, original_filename=file_name, folder_id=6)
        # insert.save()  

        try:
           insert = File.objects.filter(original_filename = file_name)
        except ObjectDoesNotExist:
           insert = File(file=files, owner=current_user, _file_size=size, uploaded_at=date, original_filename=file_name, folder_id=6)
           insert.save() 

我应该怎么做才能确保每天早上都执行上面的脚本。如何设置 cronjob / 任务调度器?

【问题讨论】:

    标签: django cron


    【解决方案1】:

    对于这样的任务,我建议你创建一个Django Management Command

    将其添加为每日 cronjob。使用以下命令打开您的 crontab:

    # crontab -e
    

    并使用如下字符串添加命令:

    @daily /path/to/manage.py <name_of_management_command>
    

    【讨论】:

    • 我认为这更容易..谢谢..我会尝试
    • 简单但不完整,因为 django 脚本不是可执行文件
    • 我在哪里可以参考每个步骤?
    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多