Linux Signals and Job Control

Linux signals, process groups, shell job control, and safe batch-termination patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Shell Job Control

Move jobs between foreground and background from interactive shells.

Start command in background

Launch a command as a background job.

bashANYjobsbackground
bash
long_task &
Notes

Launch a command as a background job.

List shell jobs

Show background and stopped jobs with PIDs.

bashANYjobsshell
bash
jobs -l
Notes

Show background and stopped jobs with PIDs.

Bring job to foreground

Resume a job in the foreground.

bashANYfgjobs
bash
fg %1
Notes

Resume a job in the foreground.

Resume stopped job in background

Continue a stopped job in the background.

bashANYbgjobs
bash
bg %1
Notes

Continue a stopped job in the background.

Suspend current foreground job

Suspend the current foreground process group.

bashANYjobssuspend
bash
# Press Ctrl+Z in the terminal
Notes

Suspend the current foreground process group.

Remove job from shell tracking

Prevent the shell from sending SIGHUP to the job on exit.

bashANYdisownjobs
bash
disown %1
Notes

Prevent the shell from sending SIGHUP to the job on exit.

Wait for PID in shell

Block until a background PID exits.

bashANYwaitshell
bash
wait 1234
Notes

Block until a background PID exits.

Wait for shell job

Block until a shell job completes.

bashANYwaitjobs
bash
wait %1
Notes

Block until a shell job completes.

Signals Reference and Delivery

Work with standard Unix signals and process groups.

Trap shell signals

Handle a signal in a shell script.

bashANYsignalstrapshell
bash
trap "echo caught INT" INT
Notes

Handle a signal in a shell script.

Signal a process group

Signal every process in a process group.

bashANYkillprocess-group
bash
kill -- -1234
Notes

Signal every process in a process group.

Signal oldest match

Signal only the oldest matching process.

bashANYpkilloldest
bash
pkill -o -TERM myworker
Notes

Signal only the oldest matching process.

Signal newest match

Signal the newest matching process.

bashANYpkillnewest
bash
pkill -n -HUP nginx
Notes

Signal the newest matching process.

Count matching processes

Count processes that match a pattern.

bashANYpgrepcount
bash
pgrep -c sshd
Notes

Count processes that match a pattern.

Signal by number

Send a custom signal to matching processes.

bashANYpkillusr1
bash
pkill -USR1 myapp
Notes

Send a custom signal to matching processes.

Test whether PID exists

Check whether a process exists and whether you can signal it.

bashANYkillexistence-check
bash
kill -0 1234
Notes

Check whether a process exists and whether you can signal it.

Batch Termination Patterns

Terminate groups of processes safely and predictably.

Kill exact process name

Only match an exact process name.

bashANYpkillexact
bash
pkill -x ssh-agent
Notes

Only match an exact process name.

Kill by parent PID

Signal all child processes of a parent.

bashANYpkillchildren
bash
pkill -P 1234
Notes

Signal all child processes of a parent.

Kill all by user and name

Limit killall by user and executable name.

bashANYkillalluser
bash
killall -u alice python
Notes

Limit killall by user and executable name.

Kill using regex

Use a regex pattern with killall where supported.

bashANYkillallregex
bash
killall -r "worker[0-9]+"
Notes

Use a regex pattern with killall where supported.

Terminate port owners with lsof and xargs

Combine lsof and xargs to terminate port users.

bashANYlsofxargskill
bash
lsof -ti :3000 | xargs -r -n1 kill -TERM
Notes

Combine lsof and xargs to terminate port users.

Recommended next

No recommendations yet.