简体   繁体   English

用于自动启动和停止 AWS EC2 实例的脚本

[英]Script for automation start and stop AWS EC2 instance

I've installed Cloudera cluster on 4 nodes Amazon EC2 instance.我在 4 个节点的 Amazon EC2 实例上安装了 Cloudera 集群。 For certain time such as Monday-Friday Night, Saturday, and Sunday, I didn't need to use those 4 nodes Amazon EC2 instance for more effective cost.对于某些时间,例如周一至周五晚上、周六和周日,我不需要使用这 4 个节点的 Amazon EC2 实例以获得更有效的成本。

How to automate start and stop an those Amazon EC2 instances using script?如何使用脚本自动启动和停止那些 Amazon EC2 实例? Could anybody give me the example of the script to do it?谁能给我一个脚本的例子来做到这一点?

Thanks,谢谢,

You can create script to stop and start instance(s) or directly run commands through crontab in linux or schedule-task in windows您可以创建脚本来停止和启动实例,或者直接通过 linux 中的crontab或 windows 中的 schedule-task运行命令

for example if you want to stop instance at 11.00 pm例如,如果您想在晚上 11 点停止实例
add below line in crontab (you will get this through crontab -e )在 crontab 中添加以下行(您将通过 crontab -e 获得)

0 23 * * * sh stop.sh 0 23 * * * sh 停止.sh

format is格式是
mh dom mon dow command mh dom mon dow 命令

for start instance对于开始实例
aws ec2 start-instances --instance-ids i-1a1234

for stop instance停止实例
aws ec2 stop-instances --instance-ids i-1a1234

I have written a small shell script to automate the starting and loggin in to my aws instances via terminal.我编写了一个小的 shell 脚本来自动启动并通过终端登录到我的 aws 实例。 You can use it你可以使用它

#!/bin/bash
aws ec2 start-instances --instance-ids i-070107834ab273992


public_ip=aws ec2 describe-instances --instance-ids i-070107834ab2 \
        --query 'Reservations[*].Instances[*].PublicDnsName' --output text


AWS_KEY="/home/debian/cs605 data management/assignment6/mumbai instance keys"

ssh -v -i "$AWS_KEY"/mumbai-instance-1.pem\
        ec2-user@$public_ip

~ ~
~ ~

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

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