运行cron作业(Node.js)时如何在Amazon Elastic Beanstalk中设置环境变量? [英] How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)?
问题描述
在Elastic Beanstalk上部署环境时,我一直在配置cron作业。这项工作的目的是每30分钟运行一次,并执行一个Node.js脚本来处理我们系统中的SMS调度。计划完成后,将通过Twilio api发送一条SMS消息。
I have been working on configuring a cron job while deploying an environment on Elastic Beanstalk. The purpose of the job is to run every 30 minutes and execute a Node.js script that processes SMS schedules in our system; when a schedule is ready, an SMS message is sent via the Twilio api.
不幸的是,Node.js环境中没有文件 / opt / elasticbeanstalk / support / envvars
包含定义为(也许我遗漏了什么?)的环境变量。
Unfortunately, Node.js environments don't have the file /opt/elasticbeanstalk/support/envvars
containing the environment variables defined (Maybe I am missing something?).
要变通解决此问题,我正在Python中从 / opt / elasticbeanstalk / bin / get-config
加载环境变量,然后执行我的Node.js脚本。
To work around this issue, I am loading the environment variables from /opt/elasticbeanstalk/bin/get-config
in Python and then executing my Node.js script.
一切都按预期进行,因此希望这可以帮助处于相同情况的某人;但是,我想知道是否有更好的方法来完成此操作...接受建议。
Everything is working as expected so hopefully this can help someone in the same situation; however, I am wondering if there is a better way to accomplish this... Open to suggestions.
在我的.ebextensions文件夹中, cron的配置文件:
In my .ebextensions folder I have the config file for the 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 to 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"
以下是作为脚本的一部分执行的Python脚本cron作业:
Here is the Python script that gets executed as part of the cron job:
#!/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/process-sms-schedules.js"])
在此先感谢您的任何反馈。
Thanks in advance for any feedback.
如何在Amazon Elastic Beanstalk(Python)中设置环境变量
推荐答案
在尝试在Elastic Beanstalk环境中执行PHP文件时遇到此问题。特别是我正在尝试执行wp-cron.php文件。
I had this issue while trying to execute a PHP file inside an Elastic Beanstalk environment. In particular I was trying to execute wp-cron.php file.
基本上,您应该编写一个cron作业,例如:
Basically you should write a cron job like 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
解释:
- 每5分钟以用户 ec2-user的身份执行命令:'* / 5 * * * * ec2-user'
- 加载elasticbeanstalk环境变量:'。 / opt / elasticbeanstalk / support / envvars;'。
- 不打印任何输出:'> / dev / null 2>& 1'。
也:
.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 as user "ec2-user": '*/5 * * * * ec2-user'.
# Loads elasticbeanstalk environment variables: '. /opt/elasticbeanstalk/support/envvars;'.
# Do not print any 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"
对于Node.js环境可能并不相同,但我可以肯定它们是相似的。
Maybe is not the same for a Node.js environment but I am pretty sure they are similar.
这篇关于运行cron作业(Node.js)时如何在Amazon Elastic Beanstalk中设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!