【问题标题】:run cron file from terminal in django从 django 的终端运行 cron 文件
【发布时间】:2013-08-05 07:09:53
【问题描述】:

您好,我有一个 django 项目,我需要从终端运行 cron 作业

下面是我的代码

 import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "noHitter.settings")

    cursor = connection.cursor()
    query="SELECT age FROM  `Player_myplayer`"
    cursor.execute(query)
    rows = cursor.fetchall()

当我尝试在终端上运行时,出现以下错误

 Could not import settings 'noHitter.settings'

请指出我在哪里做错了

【问题讨论】:

    标签: python django terminal cron


    【解决方案1】:

    实现这一点的简单方法是创建一个custom management task,然后将其添加到 crontab,例如

    crontab -e

    0 5 * * * ~/path/to/python ~/path/to/manage.py <command_name>
    

    另一种更复杂的方法是使用所需的环境变量 see this example 设置脚本。

    【讨论】:

      猜你喜欢
      • 2013-09-24
      • 2017-09-07
      • 2016-08-23
      • 1970-01-01
      • 2014-04-12
      • 2020-05-09
      • 2019-05-07
      • 2016-09-23
      • 2015-07-25
      相关资源
      最近更新 更多