AWS Elastic Beanstalk:运行Cron.d脚本,缺少环境变量

问题描述 投票:11回答:7

我正在尝试运行由cron脚本(在cron.d中)触发的PHP脚本。脚本被正确触发,但它缺少存储在$ _SERVER超全局中的Elastic Beanstalk“环境变量”。该脚本现在作为用户“root”运行,但它不在具有环境变量的相同环境中。变量设置正确,如果我从一个完整的shell运行脚本它运行得很好。

这些变量的“出口”在哪里?它们在哪里定型?我在/etc/apache/conf.d/aws_env.conf中找到了Apache的SetEnvs。我在用户的.bashrc,.bash_profile等中找不到任何内容。有解决方法吗?更好的方法吗?

谢谢。

php amazon-web-services cron elastic-beanstalk
7个回答
12
投票

在搜索同一问题的解决方案时,我遇到了这篇博文:http://sebgoo.blogspot.nl/2013/10/elastic-beanstalk-cron-command-and-rds.html。总而言之,您可以使用opt/elasticbeanstalk/support/envvars文件加载Elastic Beanstalk环境变量:

0 3 * * * . /opt/elasticbeanstalk/support/envvars; some_command

希望这可以帮助!


9
投票

我刚发现这个,使用

grep -r "export MY_VAR" /

编辑:亚马逊似乎不时移动文件位置。当前位置是:

/opt/elasticbeanstalk/support/envvars

所以我想在调用我的PHP脚本之前,我将在我的脚本中包含(源[文件路径])。看起来仍然是一种时髦的做事方式。我还在寻求更好的解决方案。

我是通过cron触发的bash脚本运行PHP的。所以为了设置环境,我会做这样的事情:

#!/bin/bash 
source /opt/elasticbeanstalk/support/envvars
php -f my-script.php

有关PHP解决方案,请参阅下面的@ userid53的答案。


4
投票

我花了几个小时试图弄清楚如何将环境变量传递给PHP CLI。我试过了:

  • 设置ebextensions配置:$ source /opt/elasticbeanstalk/support/envvars.d/sysenv
  • 将所有环境变量设置为配置文件。

无论我尝试什么,env变量都不会传递给PHP CLI。

当我以ec2-user身份登录我的EC2实例并执行此操作时:$ echo $ENVIRONMENT我得到prod。如果我这样做$ sudo su然后$ echo $ENVIRONMENT我得到prod

如果我通过脚本工作手动运行PHP CLI文件(在crontab中使用)。当它自动运行时(通过crontab)环境变量不会传递给我的脚本。

这就是我做的。把它放在你的crontab条目脚本中:

$variables = '/opt/elasticbeanstalk/support/envvars.d/sysenv';

if (file_exists($variables) && is_file($variables)) {
    $contents = file_get_contents($variables);
    foreach(explode("\n", $contents) as $line) {
        if (empty($line)) continue;

        $new_line = str_replace('export ', '', $line);
        $first_part = strpos($new_line, '=');

        $last_part = substr($new_line, $first_part, strlen($new_line));
        $variable_value = str_replace(array('=', '"'), array('',''), $last_part);

        $variable_name = substr($new_line, 0, $first_part);
        putenv($variable_name."=".$variable_value);
    }
}

它从/opt/elasticbeanstalk/support/envvars.d/sysenv文件中提取每一行,删除export部分,获取变量名称和值,并通过putenv()函数设置它。


3
投票

Laravel项目对我有用

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

对于非Laravel项目,您可以测试:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /path/to/your/script.php 1>> /dev/null 2>&1

希望这可以帮助!


1
投票

我在shell脚本中添加了以下行:

source /opt/elasticbeanstalk/support/envvars .bash_profile

所以我的脚本由crontab执行,如下所示:

#!/bin/sh
source /opt/elasticbeanstalk/support/envvars .bash_profile


# do some php stuff

1
投票

我知道这是一个旧线程,但我最近需要在Elastic Beanstalk上部署的Node.js环境中做类似的事情。据我所知,文件/opt/elasticbeanstalk/support/envvars在Node.js环境中不存在。我最后写了一个Python脚本,从/opt/elasticbeanstalk/bin/get-config加载环境变量,然后从那里执行我的Node.js脚本。

我最终使用的代码是:

#!/usr/bin/env python

import os
import subprocess
from subprocess import Popen, PIPE, call
import simplejson as json

envData = json.loads(Popen(['/opt/elasticbeanstalk/bin/get-config', 'environment'], stdout = PIPE).communicate()[0])

for k, v in envData.iteritems():
    os.environ[k] = v

call(["babel-node", "/var/app/current/<script_name>.js"])

希望这可以帮助任何需要做同样事情的人。

对于我部署的完整配置,您可以参考我原来的帖子How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)


0
投票

如果你需要类似CodeIgniter的东西:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php controller method

例:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php tasks pushNotification

更具描述性的替代方案:

* * * * * root source /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php tasks pushNotification
© www.soinside.com 2019 - 2024. All rights reserved.