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
Start command in background
long_task &

# Launch a command as a background job.

List shell jobs
jobs -l

# Show background and stopped jobs with PIDs.

Bring job to foreground
fg %1

# Resume a job in the foreground.

Resume stopped job in background
bg %1

# Continue a stopped job in the background.

Suspend current foreground job
# Press Ctrl+Z in the terminal

# Suspend the current foreground process group.

Remove job from shell tracking
disown %1

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

Wait for PID in shell
wait 1234

# Block until a background PID exits.

Wait for shell job
wait %1

# Block until a shell job completes.

## Signals Reference and Delivery
Trap shell signals
trap "echo caught INT" INT

# Handle a signal in a shell script.

Signal a process group
kill -- -1234

# Signal every process in a process group.

Signal oldest match
pkill -o -TERM myworker

# Signal only the oldest matching process.

Signal newest match
pkill -n -HUP nginx

# Signal the newest matching process.

Count matching processes
pgrep -c sshd

# Count processes that match a pattern.

Signal by number
pkill -USR1 myapp

# Send a custom signal to matching processes.

Test whether PID exists
kill -0 1234

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

## Batch Termination Patterns
Kill exact process name
pkill -x ssh-agent

# Only match an exact process name.

Kill by parent PID
pkill -P 1234

# Signal all child processes of a parent.

Kill all by user and name
killall -u alice python

# Limit killall by user and executable name.

Kill using regex
killall -r "worker[0-9]+"

# Use a regex pattern with killall where supported.

Terminate port owners with lsof and xargs
lsof -ti :3000 | xargs -r -n1 kill -TERM

# Combine lsof and xargs to terminate port users.

Recommended next

No recommendations yet.