首页 > 解决方案 > 如何在运行 nginx 的 Elastic Beanstalk 中将 Django 管理 > 命令作为 cron 作业运行

问题描述

我正在将 Django 应用程序从在 64 位 Amazon Linux 上运行的 Elastic Beanstalk Python 3.6 升级到在 64 位 Amazon Linux 2 上运行的 Python 3.8,并从 Apache 服务器迁移到 Nginx。

我正在尝试设置 cron 作业。

在 Linux 1 上我有03_files.config

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

   ...

crontab.txt

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

...

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

我已将其转换为03_cron.config

container_commands:
  01_cron_job:
    command: "cp .ebextensions/crontab.txt /etc/cron.d/autumna_cron_jobs && chmod 644 /etc/cron.d/autumna_cron_jobs"
    #leader_only: true

crontab.txt

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

...

30 0 * * * root source /var/app/venv/staging-LQM1lest/bin/activate && nice /var/app/current/manage.py test_cron
...
# this file needs a blank space as the last line otherwise it will fail

实际上,我每 10 分钟运行一次以尝试了解正在发生的事情。当我查看日志时,\var\log\cron我有:

Apr  9 09:10:01 ip-172-31-41-78 CROND[22745]: (root) CMD (source /var/app/venv/staging-LQM1lest/bin/activate && nice /var/app/current/manage.py test_cron >> /var/log/test_cron.log.log 2>&1)
Apr  9 09:20:01 ip-172-31-41-78 CROND[22842]: (root) CMD (source /var/app/venv/staging-LQM1lest/bin/activate && nice /var/app/current/manage.py test_cron >> /var/log/test_cron.log.log 2>&1)

我认为这意味着它正在尝试运行它。我没有test_cron.log档案

\var\log\messages我有

Apr  9 09:20:01 ip-172-31-41-78 systemd: Started Session 18 of user root.
Apr  9 09:20:01 ip-172-31-41-78 systemd: Starting Session 18 of user root.
Apr  9 09:20:01 ip-172-31-41-78 systemd: Started Session 17 of user root.
Apr  9 09:20:01 ip-172-31-41-78 systemd: Starting Session 17 of user root.

在其他报告中,我没有看到任何迹象表明正在发生任何事情。我不知道出了什么问题,那么如何让这些 cron 作业运行?

** 编辑 **

我已将我的 crontab 行修改如下:

* * * * * root source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron 

我已经 ssh 进入应用程序并一个接一个地运行这两个命令并且它工作 - 它应该向我发送一封电子邮件并且它确实如此。我知道它正在启动,因为在我的 cron 日志文件中我有

Apr 12 10:31:01 ip-172-31-39-9 CROND[19019]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )
Apr 12 10:32:01 ip-172-31-39-9 CROND[20926]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )
Apr 12 10:33:01 ip-172-31-39-9 CROND[21215]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )
Apr 12 10:34:01 ip-172-31-39-9 CROND[21234]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )
Apr 12 10:35:01 ip-172-31-39-9 CROND[21251]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )
Apr 12 10:36:01 ip-172-31-39-9 CROND[21296]: (root) CMD (source /var/app/venv/*/bin/activate && /var/app/current/manage.py test_cron )

但我没有收到电子邮件,也找不到任何错误。请 - 欢迎任何想法

标签: pythondjangoamazon-web-servicesnginxcron

解决方案


对于遇到类似问题的任何人,我发现了我的问题。有一些环境变量(为应用程序设置),我不知道为什么,不需要在 Linux 1 实例上设置,但在 Linux 2 实例上需要设置。一旦添加到 crontab 文件中,它就可以工作了。

我通过添加做到了.platform/hooks/postdeploy/01_copy_env.sh

#!/bin/bash

#Create a copy of the environment variable file.
cp /opt/elasticbeanstalk/deployment/env /tmp/custom_env_var #aws does not give access to /opt/elasticbeanstalk/deployment/env post deployment

#Set permissions to the custom_env_var file so this file can be accessed by any user on the instance. You can restrict permissions as per your requirements.
chmod 644 /tmp/custom_env_var

并设置其权限chmod +x .platform/hooks/postdeploy/01_copy_env.sh

然后在我添加的 crontab 文件中

#!/bin/bash  
source /tmp/custom_env_var

推荐阅读