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.
In case you need something similar for CodeIgniter:
Example:
A more descriptive alternative:
I just found this, using
EDIT: Amazon seems to move the file location from time to time. Current location is:
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:
See @userid53's answer below for PHP solution.
It's works for me with a Laravel Project
For a non Laravel Project you can test that:
Hope this helps!
While searching for solutions to the same problem I ran into this blog post: http://sebgoo.blogspot.nl/2013/10/elastic-beanstalk-cron-command-and-rds.html. To summarize, you can load the Elastic Beanstalk environment variables using the
opt/elasticbeanstalk/support/envvars
file:Hope this helps!
I spent several hours trying to figure out how to pass Environment Variables to PHP CLI. I tried:
$ source /opt/elasticbeanstalk/support/envvars.d/sysenv
No matter what I tried, env variables won't pass to PHP CLI.
When I log to my EC2 instance as ec2-user and do this:
$ echo $ENVIRONMENT
I getprod
. If I do it as$ sudo su
and then$ echo $ENVIRONMENT
I getprod
.If I manually run the PHP CLI file (used in cronjob) my script works. When it runs automatically (via cronjob) Environment Variables are not passed to my script.
Here's what I did. Put this in your cronjob entry script:
It extracts each line from
/opt/elasticbeanstalk/support/envvars.d/sysenv
file, removes theexport
part, gets the variable name & value, and sets it via putenv() function.I added the following line to my shell script:
So my script, which is executed by the crontab, looks like this: