EC2_HOME is not set. Run AWS Commands from Cron

為{幸葍}努か 提交于 2019-12-10 14:29:24

问题


Basicly I'm not sure how to get my aws commands to run in crontab. I know I need to give crontab some environmental variables so that it can run the aws commands but, I don't quite know how to do that. Has anyone been able to do this before?

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/bin
* * * * * export EC2_HOME=/opt/aws/apitools/ec2
* * * * * export JAVA_HOME=/usr/lib/jvm/jre
0 8 * * 1-5 ec2-start-instances instance_id
0 7 * * 1-5 ec2-start-instances instance_id
0 7 * * 1-5 ec2-start-instances instance_id

I am on an EC2 Amazon machine and I'm able to run the aws commands in the EC2-user shell. I'm just having trouble getting the commands to run from crontab.

The mail sent to the ec2 user says "/opt/aws/bin/ec2-start-instances: line 9: EC2_HOME: EC2_HOME is not set"


回答1:


This should work for you... Needed to setup a few systems with automated cron git pull scripts, which would then execute crontab/anacron run-parts bash scripts which called upon ec2 java tools. Normally in an amz-linux-ami (their RH clone) the creating of env vars goes as follows:

(going to stick to relevant files)

Do shell login --> source /etc/profile which will loop through /etc/profile.d and source *.{users shell extension} in this case aws-apitools-common.sh

$cat aws-apitools-common.sh 
 export AWS_PATH=/opt/aws
 export PATH=$PATH:$AWS_PATH/bin
 #Prefer JDK if present (i pulled the condition to de-clutter this info)
 export JAVA_HOME=/usr/lib/jvm/java
 export JAVA_HOME=/usr/lib/jvm/jre

Here is the relevant line:

for aws_product in $(find /opt/aws/apitools /opt/aws/amitools -maxdepth 1 -type l 2>/dev/null); do
[ -e $aws_product/environment.sh ] && source $aws_product/environment.sh; done

So the script aws-apitools-common.sh searches for apitools and amitools, then sources those tools $aws_product/environment.sh. ex: source /opt/aws/apitools/ec2/environment.sh

Now this is probably what you are looking for (/opt/aws/apitools/ec2/environment.sh):

$cat environment.sh
# Set EC2_HOME.  Called from /etc/profile.d/aws-product-common
[ -z "$EC2_HOME" ] && EC2_HOME="/opt/aws/apitools/ec2"
export EC2_HOME

In short the environmental vars you want set/export in your crontab script, and or your anacron run-parted scripts would be (personally i load in key.conf files which are generated from a git repo, and having a git pull fire off every hour, so keys and the env cat be updated just like the scripts themselves. Then the job script sources the conf file):

AWS_ACCESS_KEY="blah-blah-dingle-smith"
AWS_SECRET_KEY="yankee-doodle-shit-no-stank"
JAVA_HOME="/usr/lib/jvm/java"
EC2_HOME="/opt/aws/apitools/ec2"
EC2_URL="https://us-west-2.ec2.amazonaws.com/"
PATH="$PATH:/opt/aws/bin"  # is dir contains a symlinks of tool binaries

Cheers!




回答2:


First, you should upgrade to the new AWS CLI.

Then you can put your access keys and secret keys into ~/.aws/config, and you won't have a problem. The new tools support multiple profiles, and don't rely on env variables.

Edit: If your problem is simply setting the env for jobs run from cron, take a look at:

Where can I set environment variables that crontab will use?




回答3:


I found this answer on the AWS Developer Forum that worked for me. Just copied it here to make it easier to reach:

You can source /opt/elasticbeanstalk/support/envvars to set the usual environment variables (RDS_HOSTNAME etc). The following cron entry works for us:

30 2 * * * root . /opt/elasticbeanstalk/support/envvars && php /var/www/bin/some_command.php



来源:https://stackoverflow.com/questions/21025971/ec2-home-is-not-set-run-aws-commands-from-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!