EC2_HOME未设置。从运行的Cron AWS命令 [英] EC2_HOME is not set. Run AWS Commands from Cron

查看:292
本文介绍了EC2_HOME未设置。从运行的Cron AWS命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Basicly我不知道如何让我的AWS命令在crontab中运行。我知道我需要给crontab的一些环境变量,以便它可以运行AWS的命令,但是,我不太知道该怎么做。有没有人能之前做到这一点?

<$p$p><$c$c>PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/bin * * * * *出口EC2_HOME = /选择/ AWS / apitools / EC2 * * * * *出口JAVA_HOME = / usr / lib中/ JVM / JRE 0 8 * * 1-5 EC2-启动实例INSTANCE_ID 0 7 * * 1-5 EC2-启动实例INSTANCE_ID 0 7 * * 1-5 EC2-启动实例INSTANCE_ID

我是一个亚马逊EC2的机器上,我能够运行在EC2用户shell AWS的命令。我只是遇到了麻烦的命令,从crontab中运行。

发送到EC2用户的邮件说:/选择/ AWS /斌/ EC2-启动实例:9号线:EC2_HOME:EC2_HOME未设置

解决方案

这应该为你工作...所需设置一些系统自动cron的混帐拉脚本,然后将执行的crontab / anacron的运行部件的bash脚本它呼吁EC2 Java工具。通常在AMZ-的Linux AMI(他们RH克隆)ENV的创建瓦尔去如下:

(要坚持相关文件)

做壳登录 - > 源/ etc / profile文件将遍历/etc/profile.d中和源* {用户壳。扩展} 在这种情况下 aws-apitools-common.sh

  $猫aws-apitools-common.sh
 出口AWS_PATH = /选择/ AWS
 出口PATH = $ PATH:$ AWS_PATH /箱
 #preFER JDK如果present(我把条件去杂波此信息)
 出口JAVA_HOME = / usr / lib中/ JVM / Java的
 出口JAVA_HOME = / usr / lib中/ JVM / JRE
 

下面是相关线路:

 的aws_product在$(找到的/ opt / AWS / apitools的/ opt / AWS / amitools -maxdepth 1型L 2&GT;的/ dev / null的);做
[-e $ aws_product / environment.sh]放大器;&安培;源$ aws_product / environment.sh;做完
 

所以剧本 aws-apitools-common.sh 搜索apitools和amitools,那么源这些工具 $ aws_product / environment.sh 。例如:源/opt/aws/apitools/ec2/environment.sh

现在这可能是你所期待的( /opt/aws/apitools/ec2/environment.sh ):

  $猫environment.sh
#设置EC2_HOME。从/etc/profile.d/aws-product-common调用
[-z$ EC2_HOME]&功放;&安培; EC2_HOME =/选择/ AWS / apitools / EC2
出口EC2_HOME
 

总之你想要设置/导出在crontab脚本,或者您anacron的运行深裂脚本将是环境瓦尔(我个人装载这是从一个混帐回购协议产生key.conf文件,并具有混帐拉。断火每隔一小时,所以按键和ENV猫来就像脚本本身更新,那么作业脚本源conf文件):

  AWS_ACCESS_KEY =夸夸其谈 - 丁格尔 - 史密斯
AWS_SECRET_KEY =扬克涂鸦狗屎,不发臭
JAVA_HOME =/ usr / lib中/ JVM / Java的
EC2_HOME =/选择/ AWS / apitools / EC2
EC2_URL =htt​​ps://us-west-2.ec2.amazonaws.com/
PATH =$ PATH中:/ opt / AWS / BIN#是目录中包含的工具的二进制文件的符号链接
 

干杯!

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"

解决方案

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!

这篇关于EC2_HOME未设置。从运行的Cron AWS命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆