GGistDev

Processes and Jobs

Manage foreground/background processes, jobs, and signals.

Backgrounding and jobs

long_task &          # run in background
jobs                 # list jobs
fg %1                # bring job 1 to foreground
bg %1                # resume job 1 in background

Stopping and resuming

  • Ctrl+C sends SIGINT to foreground
  • Ctrl+Z sends SIGTSTP (stop); resume with fg or bg

nohup and disown

nohup cmd > out.log 2>&1 &    # ignore HUP (hangup)
disown %1                      # remove job from shell's job table

Keeps processes running after you log out.

Signals

kill -TERM <pid>   # polite request to terminate
kill -KILL <pid>   # force kill
trap 'echo stopping; cleanup' TERM INT

Process substitution and groups

# Start a pipeline as a group
{ producer | consumer; } &

Send signals to groups with a negative PID.

Checking status

wait %1    # wait for background job
wait $pid  # wait for PID

$! holds the PID of the most recent backgrounded process.

Summary

  • Use &, jobs, fg/bg for job control; nohup/disown to detach
  • Handle signals with trap and use wait to coordinate