diff --git a/scripts/dev.sh b/scripts/dev.sh index 5b76cab..c1adbfd 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -35,13 +35,30 @@ backend_pid=$! ( cd "$repo_root/frontend" && npm run dev ) & frontend_pid=$! +# Kill a process and its whole descendant tree (children first), portably across +# macOS and Linux — npm spawns vite as a grandchild, so killing the captured pid +# alone would orphan it on :5173. `pgrep -P` (list children) exists on both. +kill_tree() { + local pid=$1 child + for child in $(pgrep -P "$pid" 2>/dev/null); do + kill_tree "$child" + done + kill "$pid" 2>/dev/null || true +} + cleanup() { + trap - INT TERM echo echo "==> stopping backend and Vite" - kill "$backend_pid" "$frontend_pid" 2>/dev/null || true + kill_tree "$backend_pid" + kill_tree "$frontend_pid" wait "$backend_pid" "$frontend_pid" 2>/dev/null || true } trap cleanup INT TERM -wait -n "$backend_pid" "$frontend_pid" +# Wait until either process exits, then tear the other down. `wait -n` would be +# tidier but is unsupported on macOS's bundled bash 3.2, so poll portably. +while kill -0 "$backend_pid" 2>/dev/null && kill -0 "$frontend_pid" 2>/dev/null; do + sleep 1 +done cleanup