开发者

To stop the EC2 instance after the execution of a script

开发者 https://www.devze.com 2023-04-09 07:44 出处:网络
I configured a ubuntu server(AWS ec2 instance)system as a cronserver, 9 cronjobs run between 4:15-7:15 &开发者_如何转开发amp; 21:00-23:00. I wrote a cron job on other system(ec2 intance) to stop this

I configured a ubuntu server(AWS ec2 instance) system as a cronserver, 9 cronjobs run between 4:15-7:15 &开发者_如何转开发amp; 21:00-23:00. I wrote a cron job on other system(ec2 intance) to stop this cronserver after 7:15 and start again @ 21:00. I want the cronserver to stop by itself after the execution of the last script. Is it possible to write such script.


When you start the temporary instance, specify

--instance-initiated-shutdown-behavior terminate

Then, when the instance has completed all its tasks, simply run the equivalent of

sudo halt

or

sudo shutdown -h now

With the above flag, this will tell the instance that shutting down from inside the instance should terminate the instance (instead of just stopping it).


Yes, you can add an ec2stop command to the end of the last script.

You'll need to:

  • install the ec2 api tools

  • put your AWS credentials on the intance, or create IAM credentials that have authority to stop instances

  • get the instance id, perhaps from the inIstance-data

Another option is to run the cron jobs as commands from the controlling instance. The main cron job might look like this:

  • run processing instance -wait for sshd to accept connections
  • ssh to processing instance, running each processing script
  • stop processing instance

This approach gets all the processing jobs done back to back, leaving your instance up for the least amount of time., and you don't have to put the credentials on thee instance.

If your use case allows for the instance to be terminated instead of stopped, then you might be able to replace the start/stop cron jobs with EC2 autoscaling. It now sports schedules for running instances.

http://docs.amazonwebservices.com/AutoScaling/latest/DeveloperGuide/index.html?scaling_plan.html

0

精彩评论

暂无评论...
验证码 换一张
取 消