Auto Shutdown Script

March 29th, 2025
tech
I run a lot of one-off jobs on EC2 machines. This usually looks like:
  • Stand up a machine
  • Mess around for a while trying things and writing code
  • Run my command under screen
For short jobs this is fine, but when I run a long job there are two issues:
  • If the machine costs a non-trivial amount and the job finishes in the middle of the night I'm not awake to shut it down.

  • I could, and sometimes do, forget to turn the machine off.

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

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

$ prevent-shutdown long-running-command

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

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

Referenced in: Host Keys and SSHing to EC2

Comment via: facebook, lesswrong, mastodon, bluesky

Recent posts on blogs I like:

Book Review: The Kingdom, The Power, and the Glory

Against the Internet

via Thing of Things April 25, 2025

Impact, agency, and taste

understand + work backwards from the root goal • don’t rely too much on permission or encouragement • make success inevitable • find your angle • think real hard • reflect on your thinking

via benkuhn.net April 19, 2025

Which Came First, the Chicken or the Egg?

When I thought about this question it was really hard to figure out because the way it's phrased it's essentially either a chicken just pops into existence, or an egg just pops into existence, without any parent animals involved. I thought about t…

via Lily Wise's Blog Posts April 13, 2025

more     (via openring)