Doing local development on a bunch of interconnected services I often want to start multiple long running server processes.
This is the basic script I use for that.
It has the added niceness of prefixing output with an identifier so you know which process it came from.
#!/usr/bin/env bash
pids=()
cleanup() {
echo "CONTROL: Cleaning up, killing all processes"
for pid in "${pids[@]}"; do
kill "$pid"
done
exit 0
}
trap cleanup SIGINT
start_process() {
local prefix="$1"
shift
"$@" > >(sed "s/^/$prefix: /") 2> >(sed "s/^/$prefix (err): /" >&2)
pids+=($!)
}
# Place your processes here
start_process "A" python3 -m http.server 9000
start_process "B" python3 -m http.server 9001
echo "CONTROL: All processes started, streaming stdout and stderr"
wait
Tip
This makes a nice basis for a local-run.sh
script for more complex projects to get developers up and running easily.