Add error handling for lsof and pgrep commands

This commit is contained in:
Chris Wanstrath 2026-03-23 16:58:44 -07:00
parent 1f0c7bd099
commit df05cbd3aa

View File

@ -471,21 +471,29 @@ async function killStaleProcesses() {
const pids = new Set<number>() const pids = new Set<number>()
// Find processes listening on our port range // Find processes listening on our port range
const lsof = Bun.spawnSync(['lsof', '-ti', `:${MIN_PORT - 1}-${MAX_PORT}`]) try {
const lsofOutput = lsof.stdout.toString().trim() const lsof = Bun.spawnSync(['lsof', '-ti', `:${MIN_PORT - 1}-${MAX_PORT}`])
if (lsofOutput) { const lsofOutput = lsof.stdout.toString().trim()
for (const pid of lsofOutput.split('\n').map(Number)) { if (lsofOutput) {
if (pid && pid !== process.pid) pids.add(pid) for (const pid of lsofOutput.split('\n').map(Number)) {
if (pid && pid !== process.pid) pids.add(pid)
}
} }
} catch {
// lsof not available (e.g. minimal Linux installs)
} }
// Find orphaned "bun run toes" child app processes // Find orphaned "bun run toes" child app processes
const pgrep = Bun.spawnSync(['pgrep', '-f', 'bun run toes']) try {
const pgrepOutput = pgrep.stdout.toString().trim() const pgrep = Bun.spawnSync(['pgrep', '-f', 'bun run toes'])
if (pgrepOutput) { const pgrepOutput = pgrep.stdout.toString().trim()
for (const pid of pgrepOutput.split('\n').map(Number)) { if (pgrepOutput) {
if (pid && pid !== process.pid) pids.add(pid) for (const pid of pgrepOutput.split('\n').map(Number)) {
if (pid && pid !== process.pid) pids.add(pid)
}
} }
} catch {
// pgrep not available
} }
if (pids.size === 0) return if (pids.size === 0) return