Eric Hammond wrote:
> A common problem I hear from both new and experienced EC2 users is the
> mistake of forgetting to shut down instances which were started to do
> short term tasks.
> So far this year, I've probably spent $40 in charges I could have
> avoided by terminating instances when I stopped needing them. Often,
> I know when I start an instance that it is only to test a short
> process and I don't expect to have it running more than the first
> hour.
> Here's a trick which can be used to start an instance and have that
> instance shut itself down in 50 minutes:
> ec2-run-instances AMIID --key KEYPAIR --user-data '#!bin/sh
> shutdown -h +50 >/dev/null &'
> Note that this is a two line command with a newline after #!/bin/sh
> When you ssh in to the instance, you will get a repeated warning
> letting you know how long you have until the instance terminates.
> If at any point you wish to cancel the automatic shutdown and keep
> that system up, you can use the command:
> sudo shutdown -c
> I chose 50 minutes to keep the EC2 charge under a single hour allowing
> for startup and shutdown which may take a couple minutes, depending on
> the image.
> This works on any AMI which supports user-data scripts including the
> Ubuntu and Debian images I publish on http://alestic.com and the
> latest official Ubuntu beta2 images.
> --
> Eric Hammond