2

我想在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

crontab

* * * * * 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

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

4

2 回答 2

4

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

AWS 控制台 => 打开Elastic Beanstalk。选择应用程序并转到环境之一。在那里我们可以看到所有环境名称,因为我们需要进入Configuration“,然后转到软件部分并单击修改。查找特定于环境的ENV_TYPE值,它指定它是 dev、stage、demo 还是 prod,等等,下面的例子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/

于 2019-04-25T08:35:58.303 回答
0

您不能同时在 中同时使用两者test,如果这样做,则将优先。leader_onlycontainer_commandsleader_only

container_commands 上的弹性 beanstalk 文档

一个命令可以是仅领导者或具有测试,但不能两者兼而有之(leader_only 优先)。

于 2019-04-11T13:47:32.727 回答