如何在 .ebextensions config 中将条件用于 crontab 多个环境 (AWS Elastic Be



我想cron.config文件中添加一个条件。我需要检查ENV_ID(环境 ID(,如果环境 ID 与生产服务器匹配,则 cron 将在 crontab 中设置,否则 cron 不会设置检查。

cron.config

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_test:
    command: |
      ENV_ID=`{"Ref": "AWSEBEnvironmentId" }`
      ENV_NAME=`{"Ref": "AWSEBEnvironmentName" }`
  03_add_crontab:
    test:  [ $ENV_ID == "e-r19pphhp78l" ]
    command: "cat .ebextensions/crontab | crontab"
    leader_only: true

克朗塔布

* * * * * wget https://example.com/cronrun.php >> /dev/null

另外,我检查条件是否正常,但现在可以工作。

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_test:
    command: |
      ENV_ID=`{"Ref": "AWSEBEnvironmentId" }`
      ENV_NAME=`{"Ref": "AWSEBEnvironmentName" }`
      ENV_MYID="e-r19pphhp78l"
  03_add_crontab:
    command: |
      if [ $ENV_ID == $ENV_MYID ] then
        "cat .ebextensions/crontab | crontab"
      fi
    leader_only: true

我无法找到缺少的内容和错误的脚本。

我找到了这个问题的解决方案。我在 Elastic Beanstalk 中设置了所有环境值。以下是设置

AWS 控制台 =>打开Elastic Beanstalk 。选择应用程序并转到其中一个环境。在那里,我们可以看到所有环境名称,因为我们需要进入Configuration",然后转到软件部分并单击修改。查找特定于环境的ENV_TYPE值,该值指定它是开发、阶段、演示还是生产等。下面的示例使用 ENV_TYPE 进行说明。您可以根据需要创建变量并为其命名。

现在我们需要打开文件夹中cron.config文件.ebextensions并添加条件。

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_add_crontab:
    command: 'if [ "$ENV_TYPE" == "production" ]; then sudo cat .ebextensions/crontab | crontab; fi'
    command: 'if [ "$ENV_TYPE" == "development" ]; then cat .ebextensions/crontabdev | crontab; fi'
    command: 'if [ "$ENV_TYPE" == "staging" ]; then cat .ebextensions/crontabstag | crontab; fi'
    leader_only: true

欲了解更多详情,请查看此链接:-http://www.smalldaytech.com/conditional-aws-elastic-beanstack-cron-job-for-multiple-environments/

你不能在container_commands中同时使用testleader_only,如果你这样做,leader_only将优先。

弹性豆茎文档 container_commands

命令可以是仅领导者的,也可以是测试的,但不能同时具有两者(leader_only优先(。

最新更新