Php AWS Elastic Beanstalk:正在运行Cron.d脚本,缺少环境变量
我试图运行一个由cron脚本(在cron.d中)触发的PHP脚本。脚本已正确触发,但缺少存储在$\u服务器超全局中的弹性Beanstalk“环境变量”。该脚本目前正在以用户“root”身份运行,但它不在具有环境变量的同一环境中。变量设置正确,如果我从一个完整的shell运行脚本,它就可以正常运行 这些变量的“导出”在哪里?他们在哪里安家?我在/etc/Apache/conf.d/aws_env.conf中找到了Apache的setenv。我在用户的.bashrc、.bash_配置文件等中找不到任何内容。是否有解决方法?有更好的方法吗Php AWS Elastic Beanstalk:正在运行Cron.d脚本,缺少环境变量,php,amazon-web-services,cron,amazon-elastic-beanstalk,Php,Amazon Web Services,Cron,Amazon Elastic Beanstalk,我试图运行一个由cron脚本(在cron.d中)触发的PHP脚本。脚本已正确触发,但缺少存储在$\u服务器超全局中的弹性Beanstalk“环境变量”。该脚本目前正在以用户“root”身份运行,但它不在具有环境变量的同一环境中。变量设置正确,如果我从一个完整的shell运行脚本,它就可以正常运行 这些变量的“导出”在哪里?他们在哪里安家?我在/etc/Apache/conf.d/aws_env.conf中找到了Apache的setenv。我在用户的.bashrc、.bash_配置文件等中找不到任
谢谢。我刚刚用
grep -r "export MY_VAR" /
编辑:亚马逊似乎会不时移动文件位置。目前的位置是:
/opt/elasticbeanstalk/support/envvars
所以我想在调用我的php脚本之前,我只需要在脚本中包含(source[file path])。看起来仍然是一种时髦的做事方式。我仍然在寻找更好的解决方案
我通过cron触发的bash脚本运行PHP。因此,要设置环境,我将执行以下操作:
#!/bin/bash
source /opt/elasticbeanstalk/support/envvars
php -f my-script.php
#!/bin/sh
source /opt/elasticbeanstalk/support/envvars .bash_profile
# do some php stuff
有关PHP解决方案,请参见下面@userid53的答案。我花了几个小时试图找出如何将环境变量传递给PHP CLI。我试过:
- ebextensions配置中的设置:
$source/opt/elasticbeanstalk/support/envvars.d/sysenv
- 将所有我的环境变量设置为配置文件
$echo$ENVIRONMENT
我得到了prod
。如果我按照$sudo su
的方式执行,然后$echo$ENVIRONMENT
我会得到prod
如果我手动运行PHPCLI文件(在cronjob中使用),我的脚本可以工作。当它自动运行时(通过cronjob),环境变量不会传递给脚本
这就是我所做的。将其放入cronjob输入脚本中:
$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
文件中提取每一行,删除导出
部分,获取变量名称和值,并通过函数进行设置。在搜索解决我在这篇博文中遇到的同一问题时:。总之,您可以使用opt/elasticbeanstalk/support/envvars
文件加载Elastic Beanstalk环境变量:
0 3 * * * . /opt/elasticbeanstalk/support/envvars; some_command
希望这有帮助 我在shell脚本中添加了以下行:
source /opt/elasticbeanstalk/support/envvars .bash_profile
因此,由crontab执行的脚本如下所示:
#!/bin/bash
source /opt/elasticbeanstalk/support/envvars
php -f my-script.php
#!/bin/sh
source /opt/elasticbeanstalk/support/envvars .bash_profile
# do some php stuff
我知道这是一个旧线程,但我最近需要在部署在Elastic Beanstalk上的Node.js环境中执行类似的操作;据我所知,Node.js环境中不存在文件
/opt/elasticbeanstalk/support/envvars
。最后我编写了一个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"])
#/usr/bin/env python
导入操作系统
导入子流程
从子流程导入Popen、管道、调用
将simplejson导入为json
envData=json.loads(Popen(['/opt/elasticbeanstalk/bin/get-config','environment'],stdout=PIPE).communicate()[0])
对于envData.iteritems()中的k,v:
os.environ[k]=v
调用([“巴别塔节点”,“/var/app/current/.js”])
希望这能帮助任何需要这样做的人
关于我部署的完整配置,您可以参考我的原始帖子,它对我来说适用于一个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
希望这有帮助 如果您需要类似的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
示例:
* * * * * 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
更具描述性的选择:
* * * * * 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
在版本3.0.3中,它再次发生更改,请使用此命令在命令行中导出您的环境
file="/opt/elasticbeanstalk/deployment/env"
while IFS=: read -r f1
do
export $f1
done <"$file"
file=“/opt/elasticbeanstalk/deployment/env”
而IFS=:read-r f1
做
出口$f1
完成AWS和EBS不时更改环境变量的位置。目前,我能够从以下路径检索env变量(我使用了python EBS)
/opt/python/current/env
您能解释一下您是如何包含它的吗?它在.ebextensions文件中吗?我也有同样的问题。@AlexMarkov请使用此命令:$source/opt/elasticbeanstalk/support/envvars.d/sysenv
,设置到该文件的所有变量都将作为当前shell中的环境变量使用。@AlexMarkov请查看我的答案。@userid53谢谢!实际上,我已经学会了使用$HOME/.bash\u配置文件代码>在命令之前。我猜是一样的?文件被移动到/opt/elasticbeanstalk/support/envvarst,这在PHP中是一个很好的方法。我正在通过cron运行bash脚本,所以我会做一些类似于:#/bin/bash-source/opt/elasticbeanstalk/support/envvars.d/sysenv-php-f my-script.php很抱歉没有换行符。在抓挠了我几个小时之后,我省下了一天的时间!对于希望通过Artisan使用SQS运行Laravel queue worker的任何人来说,在PHP将这些环境变量放入CLI之前,您需要这样的东西来加载这些环境变量。您是如何解决权限问题的?日志文件创建为root
user,不能由webapp
user写入。谢谢!在CodeIgniter上对我非常有效