|
|
|
Background jobs let you run commands asynchronously. This is shell’s simple form of concurrency. |
|
|
Run a command in background with &: |
|
|
$! holds the PID of the last background command. |
|
|
Wait for a specific job: |
|
|
Wait for all background jobs: |
|
|
Check if a job is still running: |
|
|
Run command and capture output later: |
|
|
Bash
In bash, you can use process substitution or coproc |
|
|
Coproc example (bash 4+): |
|
|
POSIX pattern: Use temp file or named pipe |
|
|
Parallel execution pattern: |
|
|
Limit concurrent jobs: |
|
|
Signal handling for cleanup: |
|
|
Job control (interactive shells): |
|
|
In interactive bash:
Ctrl+Z suspends foreground job
|
|
|
Bash
Disown a job (keep running after shell exits): |
|
|
nohup for persistent background jobs: |
|
|
Pipeline with background processing: |
|
|
Producer-consumer pattern: |
|
|
Check job count: |
|
|
Wait with timeout (using background wait): |
|