【问题标题】:Run django management command on elastic beanstalk worker instance using crontab使用 crontab 在弹性 beanstalk worker 实例上运行 django 管理命令
【发布时间】:2019-07-31 19:40:18
【问题描述】:

我已经编写了几个我想每小时、每天和每周运行的 django 管理命令。我正在使用 Elastic Beanstalk 并创建了一个部署代码的工作实例。有人可以帮助我如何使用弹性 beantalk 使用 crontab 运行 django 管理命令。谢谢

这是我的管理命令:

python manage.py command_name

请帮我在 .ebextensions/django.config 文件中为 crontab 编写 container_command,它将每小时安排一次命令。谢谢

有什么帮助吗?

【问题讨论】:

  • 到目前为止你有什么尝试?
  • container_commands: run_cron: command: */60 * * * * python /path/to/manage.py command_name
  • 这是写在 .ebextensions/django.config 文件中的。

标签: django amazon-web-services cron amazon-elastic-beanstalk django-management-command


【解决方案1】:

.ebextensions创建文件crontab.txt

# Set the cron to run with utf8 encoding
PYTHONIOENCODING=utf8

0 1 * * * root source /opt/python/current/env && nice /opt/python/current/app/manage.py command_name
# this file needs a blank space as the last line otherwise it will fail

这是一个 crontab 文件,它将在每天凌晨 1 点运行 command_name。您需要根据您的文件结构选择正确的路径。

在您的一个配置文件中包括:

  02_cron_job:
    command: "cp .ebextensions/crontab.txt /etc/cron.d/my_cron_jobs && chmod 644 /etc/cron.d/my_cron_jobs"
    leader_only: true

这会将您的 cron 文件复制到 ec2 实例上的正确位置,以便作业运行。

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多