【问题标题】:How to start AWS EC2 instance automatically using crontab?如何使用 crontab 自动启动 AWS EC2 实例?
【发布时间】:2020-12-14 09:00:21
【问题描述】:

我正在尝试使用 crontab 自动启动 EC2 实例,但它无法正常工作。

我的 crontab 设置是这样的。

0 1 * * * aws ec2 stop-instances  --region=ap-northeast-1 --instance-ids=i-*******
0 8 * * * aws ec2 start-instances  --region=ap-northeast-1 --instance-ids=i-*******

在此设置中,“ec2 stop-instances”可以完美运行,但“ec2 start-instances”不能。 这对我来说很奇怪。 如果启动和停止功能都不起作用,我可以说服。

有谁知道问题出在哪里? 感谢您的合作。

附言

如果我手动运行“aws ec2 start-instances”,我得到的消息在下面。

{
    "StartingInstances": [
        {
            "InstanceId": "i-**********", 
            "CurrentState": {
                "Code": 16, 
                "Name": "running"
            }, 
            "PreviousState": {
                "Code": 16, 
                "Name": "running"
            }
        }
    ]
}

'aws ec2 停止实例'

{
    "StoppingInstances": [
        {
            "InstanceId": "i-024a225909929750a", 
            "CurrentState": {
                "Code": 64, 
                "Name": "stopping"
            }, 
            "PreviousState": {
                "Code": 16, 
                "Name": "running"
            }
        }
    ]
}

【问题讨论】:

  • 您检查 cron 日志是否有任何错误消息?
  • 手动运行会发生什么?
  • 您可以手动将作业的输出发送到日志文件并检查错误,例如0 1 * * * aws ec2 stop-instances --region=ap-northeast-1 --instance-ids=i-******* >>/tmp/stop-log
  • @Marcin 感谢您的评论,Marcin。我在日志文件中看不到任何有关“aws ec2 start-instances”的错误消息。但是,我可以看到一些关于“aws ec2 stop-instances”的日志,它的工作原理非常完美。 (ec2-user) CMD (aws ec2 stop-instances --region=ap-northeast-1 --instance-ids=i-********)
  • @kenlukas 感谢您的评论,kenlukas。我添加了更多关于如果我手动运行“aws ec2 start-instances”的信息。请检查一下。我希望你能找到关于这个问题的一些问题。

标签: amazon-web-services amazon-ec2 cron


【解决方案1】:

我自己解决了这个问题。当我将aws ec2 start-instances 更改为/Users/UserName/.local/bin/aws ec2 start-instances 时,它可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2011-10-29
    • 2023-03-31
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多