在不活动的情况下以编程方式停止AWS EC2 [英] Programmatically Stop AWS EC2 in case of inactivity

查看:61
本文介绍了在不活动的情况下以编程方式停止AWS EC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果开发环境中没有任何活动(例如,在闲置2个小时之后),我们是否可以停止开发环境的AWS Windows Server EC2实例?我在确定是否有任何用户虚拟连接到服务器时遇到了麻烦.

我可以通过编程轻松地在固定时间启动/停止EC2,但是为了降低服务器的成本,我试图在不使用EC2时停止它.

我的意图(或用例)是:如果在指定的时间内没有用户使用EC2,它将自动停止.开发人员可以根据需要重新启动它.

解决方案

这不是简单的任务.

Amazon EC2服务提供了具有RAM,CPU和磁盘的虚拟计算机.它可以查看CPU上的活动量,网络流量和磁盘访问权限,但它无法查看操作系统.

因此,问题变成了如何检测不活跃" .这实际上取决于操作系统并做出一些艰难的决定.例如,您的家用计算机屏幕在没有鼠标/键盘输入的定义时间后关闭,但操作系统仍在后台进行活动.如果系统正在运行诸如Web服务器之类的应用程序,并且没有Web请求,则很难知道这是因为没有请求,还是因为Web服务器正在运行,因此是活动".>

底线:没有开箱即用的功能可以执行此操作.您需要找到自己的不活动"定义,然后在操作系统中触发关机.

如果您希望通过时间表进行操作,这可能会有所帮助: 解决方案

This is not a simple task.

The Amazon EC2 service provides a virtual computer that has RAM, CPU and Disk. It can view the amount of activity on the CPU, Network traffic and disk access but it cannot see into the Operating System.

So, the problem becomes how to detect 'inactivity'. This really comes down to the operating system and making some hard decisions. For example, your home computer screen turns off after a defined time of no mouse/keyboard input but the operating system is still doing activity in the background. If the system is running an application such as a web server, and there are no web requests, it is hard to know whether this is 'inactive' because there are no requests, or 'active' because the web server is running.

Bottom line: There is no out-of-the-box feature to do this. You would need to find your own definition of 'inactivity' and then trigger a shutdown in the Operating System.

If you wish to do it via schedule, this might help: Auto-Stop EC2 instances when they finish a task - DEV Community

这篇关于在不活动的情况下以编程方式停止AWS EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆