Signals, jobs & processes
Run commands in the background, wait on them, and handle Ctrl-C gracefully. The runtime side of shell scripting.
Foreground vs background: cmd runs in the foreground — you wait for it. cmd & runs in the background — the shell returns immediately, prints the job PID, and lets you do other things. jobs lists running background jobs.
Job control: Ctrl-Z suspends the current job (sends SIGTSTP). fg resumes it in the foreground. bg resumes it in the background. kill %1 signals job number 1. wait blocks until all background jobs finish; wait %1 waits for a specific one.
Signals: Ctrl-C sends SIGINT (interrupt). Ctrl-\ sends SIGQUIT. kill <pid> sends SIGTERM by default (polite shutdown). kill -9 <pid> sends SIGKILL (cannot be caught — forceful, last resort).
Parallel execution pattern: for url in a b c; do curl "$url" &; done; wait launches 3 curls in parallel then waits for all. For bounded concurrency, reach for xargs -P (xargs -P4 -I{} curl {} < urls.txt) or GNU parallel.
trap for cleanup: tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT registers a cleanup on script exit (normal or error). trap 'echo Interrupted; exit 130' INT TERM catches Ctrl-C. Without traps, temp files pile up when scripts crash.
Grounded on https://www.gnu.org/software/bash/manual/bash.html#Job-Control