【问题标题】:Crontab cannot find AWS Credentials - linuxbox EC2Crontab 找不到 AWS 凭证 - linuxbox EC2
【发布时间】:2016-08-24 15:41:02
【问题描述】:

我创建了一个 linux 盒子,它有一个非常简单的 make bucket 命令:s3 mb s3://bucket 从提示符运行它可以正常工作。

我以我登录的用户和 sudo 的身份运行 AWS 配置。细节绝对正确,因为上面不会创建存储桶。

我从 cron 收到的错误消息是 :make_bucket failed: s3://cronbucket/ Unable to locate credentials

到目前为止,我已经用 crontab 尝试了各种方法,试图告诉它凭据在哪里,其中一些是其他解决方案的合并,这可能是导致问题的原因。

我的 crontab 看起来像:

AWS_CONFIG_FILE="/home/ec2-user/.aws/config"
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binx
0 0 * * * /usr/bin/env bash /opt/foo.sh &>> /tmp/foo.log

* * * * * /usr/bin/uptime > /tmp/uptime

* * * * * /bin/scripts/script.sh >> /bin/scripts/cronlogs/cronscript.log 2>&1

最初我只有两个工作,即制作存储桶,然后创建正常运行时间(作为健全性检查),其余的 crontab 是来自其他似乎不起作用的帖子的解决方案。

非常感谢任何建议,谢谢。

【问题讨论】:

  • 您应该使用 IAM 角色。
  • 这种特殊情况我做不到

标签: linux amazon-web-services amazon-s3 cron crontab


【解决方案1】:

问题是 cron 没有获取您的环境。有几种方法可以解决这个问题。运行包含您的配置文件的 bash 脚本。或者一个不错的简单解决方案是将其包含在 crontab 中。 (将配置文件更改为您正在使用的任何内容)

0 5 * * * . $HOME/.profile; /path/to/command/to/run

看看这个thread

【讨论】:

  • 谢谢,我现在正在尝试该解决方案,主目录已设置,.profile 为空白,因此我将 .aws/config 复制到 .profile,现在正在等待作业运行
  • 恐怕同样的错误 - 这是命令: * * * * * 。 $HOME/.profile; /bin/script/script.sh >> /bin/script/cronlogs/cronscript.log 2>&1
  • 虽然我注意到我收到的邮件试图使用 root@ip 而不是我的用户,但也许这就是问题
  • 您应该尝试包含 $HOME/.bash_profile 或 $HOME/.bashrc 取决于您的系统。甚至 /etc/profile。
  • 所以通过导出将配置(区域、访问密钥、秘密、输出)中的行添加到 bash 配置文件我猜(?)应该可以吗?
【解决方案2】:

如果您为 ECS Fargate 任务角色附加了 IAM 角色,那么此解决方案将有效 在entrypoint.sh中添加以下行

declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env

在 crontab 或 cron 文件中添加以下行

SHELL=/bin/bash
BASH_ENV=/container.env

它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2018-01-07
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2021-09-29
    相关资源
    最近更新 更多