少点错误 03月29日
Auto Shutdown Script
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

文章介绍了在 EC2 机器上运行一次性任务时,如何通过编写脚本实现自动关机功能,以解决机器空转和忘记关闭的问题。作者开发了一个名为 prevent-shutdown 的工具,该工具会监控 SSH 登录和正在运行的命令,并在满足特定条件时自动关闭机器。文章还分享了使用 LLM(如 Claude 3.7)辅助开发此工具的经验,指出 LLM 在代码生成方面具有快速迭代的优势,但也需要人工校正。

💡 自动关机需求:在 EC2 机器上运行长时间任务时,存在机器空转和忘记关闭的风险,导致不必要的成本支出。

🛠️ prevent-shutdown 工具:作者开发了一个名为 prevent-shutdown 的脚本,用于监控 SSH 登录和正在运行的任务,并根据这些状态决定是否关闭机器。该工具通过 systemd timer 每五分钟检查一次。

💻 工具的使用方法:用户只需在 EC2 机器上运行 long-running-command 命令,同时使用 prevent-shutdown 工具,即可实现自动关机功能。需要使用 screen 或类似工具来保持 long-running-command 命令的持续运行。

🤖 LLM 辅助开发:作者使用 LLM(Claude 3.7)辅助编写 prevent-shutdown 工具,虽然 LLM 生成的代码存在错误,但其快速迭代的特性大大提高了开发效率。

Published on March 29, 2025 1:10 PM GMT

I run a lot of one-off jobs on EC2 machines. Thisusually looks like:

For short jobs this is fine, but when I run a long job there are twoissues:

Ideally I could tell the machine to shut itself off if no one waslogging in and there weren't any active jobs.

I didn't see anything like this (though I didn't look very hard) so Iwrote something (github):

$ prevent-shutdown long-running-command

As long as that command is still running, or someone is logged in overssh, the machine will stay on. Every five minutes a systemd timerwill check if this is the case, and if not shut the machine down.Note that you still need screen or something to preventthe long running command from exiting when you log out.

(This is an example of the kind of thing that I find goes a lot fasterwith anLLM. I used Claude 3.7, prompted it with essentially thebeginning of this blog post, took the scripts it generated as astarting point, and then fixed some things. It did make some mistakes(the big ones: a typo of $ for $$, a regexlooking for PID: that should have looked for^PID:, didn't initially plan for handling stale jobs) butthat's also about what I'd expect if I'd asked a junior engineer towrite this for me. And with much faster turnaround on my codereviews!)



Discuss

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

EC2 自动关机 脚本 LLM 云计算
相关文章