How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)? -
i have been working on configuring cron job while deploying environment on elastic beanstalk. purpose of job run every 30 minutes , execute node.js script processes sms schedules in our system; when schedule ready, sms message sent via twilio api.
unfortunately, node.js environments don't have file /opt/elasticbeanstalk/support/envvars
containing environment variables defined (maybe missing something?).
to work around issue, loading environment variables /opt/elasticbeanstalk/bin/get-config
in python , executing node.js script.
everything working expected can in same situation; however, i wondering if there better way accomplish this... open suggestions.
in .ebextensions folder have config file cron:
files: "/etc/cron.d/process-sms-schedules": mode: "000644" owner: root group: root content: | */30 * * * * root /usr/local/bin/process-sms-schedules.sh "/usr/local/bin/process-sms-schedules.sh": mode: "000755" owner: root group: root content: | #!/bin/bash # execute script process schedules python /var/app/current/process-sms-schedules.py > /var/log/process-sms-schedules.log exit 0 commands: remove_old_cron: command: "rm -f /etc/cron.d/*.bak"
here python script gets executed part of cron job:
#!/usr/bin/env python import os import subprocess subprocess import popen, pipe, call import simplejson json envdata = json.loads(popen(['/opt/elasticbeanstalk/bin/get-config', 'environment'], stdout = pipe).communicate()[0]) k, v in envdata.iteritems(): os.environ[k] = v call(["babel-node", "/var/app/current/process-sms-schedules.js"])
thanks in advance feedback.
references
how set environment variable in amazon elastic beanstalk (python)
i had issue while trying execute php file inside elastic beanstalk environment. in particular trying execute wp-cron.php file.
basically should write cron job this:
/etc/cron.d/wp-cronjob :
path=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin */5 * * * * ec2-user . /opt/elasticbeanstalk/support/envvars; /usr/bin/php /var/www/html/wp-cron.php > /dev/null 2>&1
explained:
- every 5 minutes executes commands user "ec2-user": '*/5 * * * * ec2-user'
- loads elasticbeanstalk environment variables: '. /opt/elasticbeanstalk/support/envvars;'.
- do not print output: '> /dev/null 2>&1'.
also:
.ebextensions/wp-cronjob.txt
# load paths path=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin # every 5 minutes executes commands user "ec2-user": '*/5 * * * * ec2-user'. # loads elasticbeanstalk environment variables: '. /opt/elasticbeanstalk/support/envvars;'. # not print output: '> /dev/null 2>&1'. */5 * * * * ec2-user . /opt/elasticbeanstalk/support/envvars; /usr/bin/php /var/www/html/wp-cron.php > /dev/null 2>&1
.ebextensions/cronjob.config
container_commands: add_wp_cronjob: command: "cat .ebextensions/wp-cronjob.txt > /etc/cron.d/wp-cronjob && chmod 644 /etc/cron.d/wp-cronjob" leader_only: true commands: remove_old_cron: command: "rm -f /etc/cron.d/*.bak"
maybe not same node.js environment pretty sure similar.
Comments
Post a Comment