简体   繁体   English

AWS CLI 在 cron 中不起作用

[英]AWS CLI does not work in cron

I'm trying to use the aws cli cp command in a cron of an aws environment on a Ubuntu 14.04.3 AWS EC2.我正在尝试在 Ubuntu 14.04.3 AWS EC2 上的 aws 环境的 cron 中使用 aws cli cp 命令。

The ec2-user is called ubuntu and lives in /home/ubuntu ec2-user 名为 ubuntu,位于 /home/ubuntu

I have my aws config file in /home/ubuntu/.aws/config我在 /home/ubuntu/.aws/config 中有我的 aws 配置文件

[default]
output=json  
region=eu-central-1

I have my aws credentials file in /home/ubuntu/.aws/credentials我在 /home/ubuntu/.aws/credentials 中有我的 aws 凭证文件

[default]
aws_access_key_id=******
aws_secret_access_key=******

My crontab looks like this我的 crontab 看起来像这样

* * * * * sh /home/ubuntu/test.sh

The shell script tries to copy a test file over to S3 is a one-liner: shell 脚本尝试将测试文件复制到 S3 是单行的:

/usr/local/bin/aws s3 cp test.txt s3://<my-bucket>/test.txt >> /home/ubuntu/some-log-file.log

The cron runs the script each minute, but nothing is copied to the S3 bucket. cron 每分钟运行一次脚本,但不会将任何内容复制到 S3 存储桶。

If i run the script manually on my shell it works.如果我在我的 shell 上手动运行脚本,它就可以工作。

I tried (without success):我试过(没有成功):

Putting the right path in front of aws (/usr/local/bin/aws)将正确的路径放在 aws 前面(/usr/local/bin/aws)

Putting aws_access_key_id and aws_secret_access_key into the .aws/config file as well.也将 aws_access_key_id 和 aws_secret_access_key 放入 .aws/config 文件中。

Putting aws env vars to crontab and/or shell script将 aws env vars 放入 crontab 和/或 shell 脚本

AWS_DEFAULT_REGION=eu-central-1
AWS_ACCESS_KEY_ID=******
AWS_SECRET_ACCESS_KEY =******

Defining HOME in the crontab and/or shell script在 crontab 和/或 shell 脚本中定义 HOME

HOME="/home/ubuntu"

Putting the config and credential file location to the crontab将配置和凭证文件位置放到 crontab

AWS_CONFIG_FILE="/home/ubuntu/.aws/config"
AWS_CREDENTIAL_FILE="/home/ubuntu/.aws/credentials"

Putting PATH to the crontab and/or the shell script将 PATH 放入 crontab 和/或 shell 脚本

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:"

Has anybody an idea what I might do wrong?有谁知道我可能做错了什么?

Found out that I forgot an absolute path to test.txt (/home/ubuntu/test.txt)发现我忘记了test.txt的绝对路径(/home/ubuntu/test.txt)

I'll keep the question because it lists several options and might still be helpful to others.我会保留这个问题,因为它列出了几个选项,可能仍然对其他人有帮助。

Fix was relatively simple.修复相对简单。 When running AWS CLI commands from cron you need to set the user environment variables.从 cron 运行 AWS CLI 命令时,您需要设置用户环境变量。

In the cron command use .在 cron 命令中使用 . $HOME/.profile; $HOME/.profile;

Example:示例:

10 5 * * * . $HOME/.profile;  /var/www/rds-scripts/clonedb.sh

In the shell script set the $SHELL and $PATH variables.在 shell 脚本中设置 $SHELL 和 $PATH 变量。

export SHELL=/bin/bash
export PATH=/usr/local/sbain:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

With these changes the AWS CLI is able to load the user credential files and locate the AWS CLI binary files.通过这些更改,AWS CLI 能够加载用户凭证文件并找到 AWS CLI 二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM