简体   繁体   English

AWS CLI 在 shell 中工作但在 cron 作业中不工作

[英]AWS CLI working in shell but not working in cron job

I tried to automate some jobs through cron on EC2.我试图通过 EC2 上的 cron 自动化一些工作。 However, the aws commands failed due to an error '403 Forbidden'.但是,aws 命令由于错误“403 Forbidden”而失败。 The codes worked fine when I ran those in the shell.当我在 shell 中运行这些代码时,这些代码运行良好。 I tried almost all the solutions on stack overflow but none of those solved the issue.我尝试了几乎所有关于堆栈溢出的解决方案,但没有一个解决了问题。

The testing code is simple enough.测试代码很简单。 (test copying a file from S3 to my EC2) (测试将文件从 S3 复制到我的 EC2)

/2 * * * * /usr/local/bin/aws s3 cp s3://fraud-training-test/FraudAWS_v1.R /home/ubuntu

Anyone has a sense of what else I could be doing to solve this issue?任何人都知道我还能做些什么来解决这个问题? Thanks!谢谢!

So far, I tried the following solutions:到目前为止,我尝试了以下解决方案:

  1. Installed and configured awscli for both root and my current user;为 root 和我当前的用户安装和配置了 awscli;
  2. Put the global path and home on top of my script将全局路径和主页放在我的脚本之上

eg:例如:

SHELL=/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  1. Tried scheduling the cron job as my current user and as root but both failed.尝试以我的当前用户和 root 身份安排 cron 作业,但都失败了。

Below is the exact error I got from the log:以下是我从日志中得到的确切错误:

Subject: /usr/local/bin/aws s3 cp s3://fraud-training-test/FraudAWS_v1.R /r$
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <PATH=/usr/bin:/usr/local/bin>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>

fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden

The problem is probably that the user you configure in crontab is not the same user configured in the AWS CLI.问题可能是您在 crontab 中配置的用户与 AWS CLI 中配置的用户不同。

You must match the user that executes crontab -e with the user that is configured in aws configure您必须将执行 crontab -e 的用户与 aws configure 中配置的用户匹配

This worked fine on my Lightsail Bitnami instance.这在我的 Lightsail Bitnami 实例上运行良好。

Check your AWS Installed Path by using which aws and do ls -la to know the user permissions.使用which aws和 do ls -la检查您的 AWS 安装路径以了解用户权限。

Then run it accordingly in either crontab -e for current user or sudo crontab -e for root user.然后,在任一相应的运行它crontab -e当前用户或sudo crontab -e根用户。

It worked for me in Ubuntu 16.04.它在 Ubuntu 16.04 中对我有用。

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

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