Automatically Shutting Down Idle AWS Linux Instances
Let me guess: you forgot to turn off an EC2 instance that you’ve created to run a quick test only. Depending on how big the instance is and how long it took you to realize, it can be a costly mistake. And if you memory is as bad as mine, you have been there a couple of times …
To solve this problem, I created a script that turns off the machine after a given time without any SSH sessions:
[gist 20a89197200f95d30b38]
The time is passed as a parameter (in minutes). I haven’t tested in all distros, but it should be fine with RHEL-based ones (Centos, Amazon Linux etc). Just put it in the root’s crontab with a higher frequency than the desired timeout.. To check every minute and shut down after 30 minutes, for example, add the following line to root’s crontab (sudo crontab -e
):
*/1 * * * * /home/centos/autoshutdown.sh 30
Change the path to where the script is saved. Also make sure you have the permission to execute it
And please don’t forget to remove it before going to production!
[yasr_visitor_votes size=”small”]