且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-15 17:12:34

只是发现这个,使用

  grep -rexport MY_VAR/ 
pre>

编辑:查看文件位置的注释,亚马逊似乎不时移动。



并找到:

  /opt/elasticbeanstalk/support/envvars.d/sysenv 

所以我想在调用我的php脚本之前在我的脚本中包含(source [文件路径]) 。仍然看起来像一个时髦的方式做事情。我仍然在寻找更好的解决方案。



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

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

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


I'm trying to run a PHP script that is triggered by a cron script (in cron.d). The script is triggered properly but it is missing the Elastic Beanstalk "Environment Variables" that are stored in the $_SERVER superglobal. The script is being run as the user "root" for now, but it's not in the same environment that has the environment variables. The variables are set correctly, if I run the script from a full shell it runs just fine.

Where are the "exports" for these variables? Where do they get set? I found the SetEnvs for Apache in /etc/apache/conf.d/aws_env.conf. I can't find anything in the user's .bashrc, .bash_profile, etc. Is there a workaround? A better way to do this?

Thanks.

I just found this, using

grep -r "export MY_VAR" /

EDIT: See the comments for the file location, Amazon seems to move this from time to time.

And found:

/opt/elasticbeanstalk/support/envvars.d/sysenv

So I think I'll just include (source [file path]) that in my script before calling my php script. Still seems like a funky way to do things. I'm still in for better solutions.

I was running PHP via bash script triggered by cron. So to setup the environment, I would do something like this:

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

See @userid53's answer below for PHP solution.