Email or username:

Password:

Forgot your password?
Simon Willison

A tiny TIL: how run multiple servers in a single Bash script, such that when the Bash script is terminated with Ctrl+C all of the servers are terminated as well
til.simonwillison.net/bash/mul

12 comments
Daniel Schildt

@simon Good idea. ☺️

You might also want to add the Bash "strict mode" to the script to avoid potential problems: github.com/d2s/dot/blob/43071d

Mike Mol

@simon great! next up, work out how to set up a pipeline of bash-parented PIDs, with the stout of each fed to the stdin of the next, and with each subprocess automatically restarted and replumbed on any abnormal edit.

Bash is scary powerful. About the only thing I couldn't do in it is use arrays without leaking memory...

Jonathan Carroll

@simon I'm loving your TIL format! This one made my (unpublished) list recently: redsymbol.net/articles/bash-ex

Matt Burke

@simon you have no idea how many google searches I sent this week looking for something like this for windows.

Asked ChatGPT to convert to powershell: chat.openai.com/share/952e5a0f

Excited to try it tomorrow. Thanks!

Noah K

@simon if you trap on the pseudo signal EXIT it will kick in on other exit modes too :)

Simon Willison

@coderanger Got it - that looks like this: trap cleanup EXIT

Noah K

@hynek @simon to be fair, bash is probably the most poorly documented programming language in existence.

Hynek Schlawack

@coderanger @simon I was gonna quip that Bash is a prime example where ChatGPT is a bad choice because 99% of Bash scripts out there are dangerous, hot garbage.

Noah K

@hynek @simon long ago I worked on an install script that had to work on a half dozen different unixes. It started with a comment like “if you are under 30, please consult an old person before trying any optimizations here”. Because bash on aix vs linux vs macos was such a mess.

Noah K

@simon same idea but trap functionname EXIT, and it will run on any non sigkill exit

Go Up