Surviving the terminal close
A normal background job (./script &) dies when its parent shell exits, because the shell sends SIGHUP. Three ways to keep the job alive after you close the terminal:
nohup ./script &— start with no-hangup. SIGHUP is ignored. Output goes tonohup.outin the current dir../script & disown— start, then detach the most recent job from the shell's job table.setsid ./script &— start in a new session (decoupled from the controlling terminal entirely).
Don't forget the streams
If the script still writes to the original stdout/stderr (a pipe to the terminal), it'll get SIGPIPE when that terminal closes. Always redirect:
nohup ./script > out.log 2>&1 &The right tool for long-running stuff
For things you'll come back to: tmux new -s build './script.sh' + tmux attach -t build later. The session keeps a pseudo-terminal alive even when nobody's looking.
For real services: macOS launchd or Linux systemd unit files. The Pippa stack uses launchd to keep pippa serve alive.
Check if a backgrounded job is alive
kill -0 PID && echo alive
ps -p PIDkill -0 sends signal 0, which is "check if you can" rather than "actually do it." Returns 0 if the process exists.