AWS Elastic Beanstalk: Running Cron.d script, missing Environment Variables

前端 未结 9 1181
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 10:39

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 Variab

相关标签:
9条回答
  • 2021-01-17 10:56

    I know this is an old thread but I recently needed to do something similar in a Node.js environment deployed on Elastic Beanstalk; for what I could tell, the file /opt/elasticbeanstalk/support/envvars is not present on Node.js environments. I ended up writing a Python script that loaded the environment variables from /opt/elasticbeanstalk/bin/get-config, and then executing my Node.js script from there.

    The code I ended up using was:

    #!/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"])
    

    Hope this helps anyone needing to do the same.

    For the complete configuration I deployed, you can refer to my original post How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)

    0 讨论(0)
  • 2021-01-17 10:59

    I spent several hours trying to figure out how to pass Environment Variables to PHP CLI. I tried:

    • setting in ebextensions config: $ source /opt/elasticbeanstalk/support/envvars.d/sysenv
    • setting all my Environment variables to config file.

    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 get prod. If I do it as $ sudo su and then $ echo $ENVIRONMENT I get prod.

    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:

    $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);
        }
    }
    

    It extracts each line from /opt/elasticbeanstalk/support/envvars.d/sysenv file, removes the export part, gets the variable name & value, and sets it via putenv() function.

    0 讨论(0)
  • 2021-01-17 11:01

    in version 3.0.3 it has changed again, use this to export your envars in command line

    file="/opt/elasticbeanstalk/deployment/env"
            while IFS=: read -r f1
            do
                export $f1
            done <"$file"
    
    0 讨论(0)
  • 2021-01-17 11:01

    AWS and EBS are changing the location of the env vars from time to time. At the current time, I was able to retrieve the env vars from the following path (I've python EBS)

    /opt/python/current/env

    0 讨论(0)
  • 2021-01-17 11:02

    It's works for me with a Laravel Project

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

    For a non Laravel Project you can test that:

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

    Hope this helps!

    0 讨论(0)
  • 2021-01-17 11:12

    I added the following line to my shell script:

    source /opt/elasticbeanstalk/support/envvars .bash_profile
    

    So my script, which is executed by the crontab, looks like this:

    #!/bin/sh
    source /opt/elasticbeanstalk/support/envvars .bash_profile
    
    
    # do some php stuff
    
    0 讨论(0)
提交回复
热议问题