5

I'm writing a script which shuts down EC2 instances remotely using ec2-terminate-instances. I would like to perform orderly shutdowns (similar to the ones triggered by Linux 'shutdown' command) instead of simply turning the power off. This would give me time to shut down running services and properly unmount an EC2 volume.

Unfortunately, I can't log into these instances using SSH without doing a lot of extra work. Is there an easy way to trigger a remote, orderly shutdown? Or will I need to write some sort of control daemon that lives on the server, and which I can poke when I want the server to shut down?

3 Answers 3

6

EC2 instances that have been either stopped or terminated using the API will be sent appropriate commands that can be intercepted by power daemons and used to initiate a shutdown. Most AMI's and default operating systems already know what to do when the get these signals. It is the same on desktop computers where the power button sends a special message requesting a shut down. The API terminate command does not such "pull the plug" on the instance.

2

Take a look at ec2-stop-instances! This does a shutdown -h now and sends the instance into stopped state, but does not terminate the instance. If you want to terminate the instance, use ec2-terminate-instances after this.

3
  • Does ec2-stop-instances work for instances using instance storage, or does it only work for EBS-backed instances?
    – emk
    Apr 11, 2011 at 15:10
  • Performing this operation on an instance that uses an instance store as its root device returns an error.
    – heiko
    Apr 11, 2011 at 15:30
  • When I lookup the documentation for ec2-stop-instances, I don't see anything about "shutdown -h"
    – philipkd
    Jun 3, 2023 at 1:47
1

As it turns out, ec2-terminate-instances does perform an orderly shutdown. Here's what I saw when I ran tail -f /var/log/messages on system during a remote shutdown:

Apr 11 15:11:40 domU-12-31-39-09-9E-53 shutdown[22770]: shutting down for system halt
The system is going down for system halt NOW!9E-53 (Mon Apr 11 15:11:40 2011)
Apr 11 15:11:40 domU-12-31-39-09-9E-53 init: Switching to runlevel: 0
Apr 11 15:11:41 domU-12-31-39-09-9E-53 avahi-daemon[988]: Got SIGTERM, quitting.
Apr 11 15:11:41 domU-12-31-39-09-9E-53 avahi-daemon[988]: Leaving mDNS multicast group on interface eth0.IPv6 with address fe80::1031:39ff:fe09:9e53.
Apr 11 15:11:41 domU-12-31-39-09-9E-53 avahi-daemon[988]: Leaving mDNS multicast group on interface eth0.IPv4 with address 10.210.161.157.
Connection to 50.17.174.93 closed by remote host.
Connection to 50.17.174.93 closed.

So there's no need to do anything special. The instance will shut down in an orderly fashion and disappear when it's done.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .