Process Management¶
Monitoring and controlling processes on Unix systems.
Viewing Processes¶
ps - Process Status¶
ps # Current terminal processes
ps aux # All processes (BSD style)
ps -ef # All processes (POSIX style)
Common Options (BSD style)¶
ps aux # All processes, user-oriented
ps auxf # With process tree (Linux)
ps aux --sort=-%mem # Sort by memory
ps aux --sort=-%cpu # Sort by CPU
ps aux | head -20 # Top 20 processes
Output Columns¶
| Column | Meaning |
|---|---|
| USER | Process owner |
| PID | Process ID |
| %CPU | CPU usage |
| %MEM | Memory usage |
| VSZ | Virtual memory (KB) |
| RSS | Resident memory (KB) |
| TTY | Terminal |
| STAT | Process state |
| START | Start time |
| TIME | CPU time |
| COMMAND | Command |
Process States¶
| State | Meaning |
|---|---|
| R | Running |
| S | Sleeping (interruptible) |
| D | Sleeping (uninterruptible) |
| T | Stopped |
| Z | Zombie |
Finding Specific Processes¶
ps aux | grep nginx # Find nginx processes
ps aux | grep -v grep | grep nginx # Exclude grep itself
pgrep nginx # Get PIDs only
pgrep -l nginx # With names
pgrep -a nginx # Full command line
Process Tree¶
pstree # All processes as tree
pstree -p # With PIDs
pstree -u # With usernames
pstree user # Processes of user
pstree -p $$ # Current shell tree
Interactive Monitors¶
top¶
Real-time process monitor:
top Keybindings¶
| Key | Action |
|---|---|
q | Quit |
k | Kill process |
r | Renice process |
M | Sort by memory |
P | Sort by CPU |
T | Sort by time |
c | Toggle full command |
1 | Toggle per-CPU stats |
h | Help |
top Options¶
top -o cpu # Sort by CPU (macOS)
top -o %CPU # Sort by CPU (Linux)
top -p 1234 # Monitor specific PID
top -u username # Processes by user
htop¶
Better interactive monitor (needs install):
Features:
- Color-coded display
- Mouse support
- Horizontal and vertical scrolling
- Tree view
- Easy process management
btop¶
Modern, beautiful system monitor:
See Modern Replacements for details.
System Resources¶
Memory¶
Disk I/O¶
CPU¶
Terminating Processes¶
kill¶
Send signals to processes:
kill PID # Send SIGTERM (graceful)
kill -9 PID # Send SIGKILL (force)
kill -15 PID # SIGTERM explicitly
kill -HUP PID # SIGHUP (reload config)
kill -STOP PID # Pause process
kill -CONT PID # Resume process
Common Signals¶
| Signal | Number | Purpose |
|---|---|---|
| SIGHUP | 1 | Hangup / reload |
| SIGINT | 2 | Interrupt (Ctrl+C) |
| SIGQUIT | 3 | Quit with core dump |
| SIGKILL | 9 | Force kill (cannot catch) |
| SIGTERM | 15 | Graceful termination |
| SIGSTOP | 19 | Pause (cannot catch) |
| SIGCONT | 18 | Continue |
killall¶
Kill by name:
killall nginx # Kill all nginx processes
killall -9 nginx # Force kill all
killall -u user # Kill user's processes
pkill¶
Kill by pattern:
pkill nginx # Kill matching processes
pkill -9 "python.*script" # Force kill with regex
pkill -u user # Kill by user
pkill -t pts/0 # Kill by terminal
Graceful Shutdown¶
Job Control¶
Background Jobs¶
jobs¶
List background jobs:
fg and bg¶
Ctrl+Z # Suspend current job
bg # Resume in background
bg %1 # Resume job 1 in background
fg # Bring to foreground
fg %1 # Bring job 1 to foreground
Job Specifiers¶
| Specifier | Meaning |
|---|---|
%1 | Job number 1 |
%+ or %% | Current job |
%- | Previous job |
%string | Job starting with string |
%?string | Job containing string |
nohup¶
Continue running after logout:
disown¶
Remove from shell's job table:
Wait for Processes¶
wait¶
command1 &
command2 &
wait # Wait for all background jobs
wait $! # Wait for last background job
wait %1 # Wait for job 1
Check Process Running¶
# Check if PID exists
kill -0 PID 2>/dev/null && echo "Running"
# Wait for process to end
while kill -0 PID 2>/dev/null; do
sleep 1
done
echo "Process ended"
Process Priority¶
nice¶
Start with adjusted priority:
nice command # Default nice (10)
nice -n 10 command # Nice value 10
nice -n -10 command # Higher priority (needs root)
nice -n 19 command # Lowest priority
Nice values: -20 (highest priority) to 19 (lowest priority)
renice¶
Change running process priority:
renice 10 PID # Set nice to 10
renice -n 10 -p PID # Same
renice -n -5 -p PID # Higher priority (needs root)
renice -n 10 -u user # All processes of user
Resource Limits¶
ulimit¶
View and set resource limits:
ulimit -a # Show all limits
ulimit -n # Open files limit
ulimit -u # Max processes
ulimit -v # Virtual memory
ulimit -n 4096 # Set open files limit
Practical Patterns¶
Kill All User Processes¶
Find Memory Hogs¶
Find CPU Hogs¶
Watch Process¶
Start Process and Get PID¶
Timeout Command¶
timeout 60 long_command # Kill after 60 seconds
timeout --signal=KILL 60 command # SIGKILL after timeout
Parallel Processing¶
# Process files in parallel
for file in *.txt; do
process "$file" &
done
wait
# Limit parallelism
max_jobs=4
for file in *.txt; do
while [[ $(jobs -r -p | wc -l) -ge $max_jobs ]]; do
sleep 0.1
done
process "$file" &
done
wait
Daemon Pattern¶
#!/usr/bin/env bash
run_daemon() {
while true; do
do_work
sleep 60
done
}
# Daemonize
run_daemon &
echo $! > /var/run/mydaemon.pid
disown
Try It¶
-
List processes:
-
Job control:
-
Monitor:
-
Find resource usage:
Summary¶
| Task | Command |
|---|---|
| List processes | ps aux |
| Find by name | pgrep name |
| Interactive monitor | top, htop, btop |
| Graceful kill | kill PID |
| Force kill | kill -9 PID |
| Kill by name | killall name |
| Background job | command & |
| List jobs | jobs |
| Foreground | fg %N |
| Background | bg %N |
| Ignore hangup | nohup command & |
| Remove from shell | disown |
| Wait for jobs | wait |
| Set priority | nice -n N command |
| Change priority | renice N PID |