Process Management
What is a Process?
A process is a running instance of a program. Each process has a unique process ID (PID) and occupies certain system resources (CPU, memory, etc.).
Process Related Concepts
Process States
| State | Symbol | Description |
|---|---|---|
| Running | R | Running or in run queue |
| Sleeping | S | Interruptible sleep, waiting for events |
| Uninterruptible Sleep | D | Uninterruptible sleep, usually waiting for I/O |
| Stopped | T | Stopped by signal |
| Zombie | Z | Terminated but not yet collected by parent process |
Process Types
- Foreground process: Occupies terminal until it completes
- Background process: Runs in background without occupying terminal
- Daemon process: System service that runs in background for long time
Viewing Processes
ps - Process Snapshot
bash
# Current terminal processes
$ ps
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
5678 pts/0 00:00:00 ps
# All processes (standard syntax)
$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan09 ? 00:00:01 /sbin/init
root 2 0 0 Jan09 ? 00:00:00 [kthreadd]
maxwell 1234 1233 0 10:00 pts/0 00:00:00 bash
# All processes (BSD syntax)
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169512 11284 ? Ss Jan09 0:01 /sbin/init
maxwell 1234 0.0 0.0 21388 5364 pts/0 Ss 10:00 0:00 bash
# Display process tree
$ ps -ef --forest
$ ps auxf
# Specify display columns
$ ps -eo pid,ppid,user,%cpu,%mem,stat,cmd
# Sort by CPU usage
$ ps aux --sort=-%cpu | head
# Sort by memory usage
$ ps aux --sort=-%mem | head
# Find specific process
$ ps aux | grep nginx
$ ps -ef | grep nginxps Output Explanation
| Column | Description |
|---|---|
| PID | Process ID |
| PPID | Parent process ID |
| USER | Process owner |
| %CPU | CPU usage |
| %MEM | Memory usage |
| VSZ | Virtual memory size |
| RSS | Actual memory size |
| TTY | Terminal |
| STAT | Process state |
| START | Start time |
| TIME | CPU time |
| COMMAND | Command |
top - Dynamic Process Monitoring
bash
$ top
top - 10:30:00 up 5 days, 3:00, 2 users, load average: 0.15, 0.10, 0.05
Tasks: 200 total, 1 running, 199 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.0 us, 1.0 sy, 0.0 ni, 96.5 id, 0.5 wa, 0.0 hi, 0.0 si
MiB Mem : 16000.0 total, 8000.0 free, 4000.0 used, 4000.0 buff/cache
MiB Swap: 2000.0 total, 2000.0 free, 0.0 used. 11000.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 maxwell 20 0 500000 50000 30000 S 5.0 0.3 0:10.50 firefox
5678 root 20 0 100000 10000 5000 S 2.0 0.1 0:05.00 systemdtop Interactive Commands
| Key | Function |
|---|---|
q | Quit |
h | Help |
P | Sort by CPU |
M | Sort by memory |
N | Sort by PID |
T | Sort by runtime |
k | Kill process |
r | Change priority |
u | Show specific user |
1 | Show each CPU |
c | Show full command |
s | Modify refresh interval |
htop - Enhanced top
bash
# Install
$ sudo apt install htop
# Run
$ htopFeatures:
- Color display
- Mouse support
- Vertical and horizontal scrolling
- Tree view
- Direct search and filtering
pgrep - Find Process by Name
bash
# Find PID by name
$ pgrep nginx
1234
5678
# Show process name
$ pgrep -l nginx
1234 nginx
5678 nginx
# Full command match
$ pgrep -f "python script.py"
# Find by user
$ pgrep -u maxwell
# Newest process
$ pgrep -n nginx
# Oldest process
$ pgrep -o nginxpidof - Get PID
bash
$ pidof nginx
1234 5678Controlling Processes
kill - Send Signals
bash
# Default send SIGTERM (15)
$ kill PID
$ kill 1234
# Force terminate SIGKILL (9)
$ kill -9 PID
$ kill -KILL PID
# Reload configuration SIGHUP (1)
$ kill -1 PID
$ kill -HUP PID
# Send to multiple processes
$ kill 1234 5678 9012Common Signals
| Signal | Number | Description |
|---|---|---|
| SIGHUP | 1 | Hang up, commonly used to reload configuration |
| SIGINT | 2 | Interrupt (Ctrl+C) |
| SIGQUIT | 3 | Quit (Ctrl+) |
| SIGKILL | 9 | Force terminate, cannot be caught |
| SIGTERM | 15 | Terminate, default signal |
| SIGSTOP | 19 | Stop process |
| SIGCONT | 18 | Continue running |
bash
# List all signals
$ kill -lkillall - Kill by Name
bash
# Terminate all processes with same name
$ killall nginx
# Force terminate
$ killall -9 nginx
# Interactive confirmation
$ killall -i nginx
# Kill by user
$ killall -u maxwellpkill - Kill by Pattern
bash
# Kill by name
$ pkill nginx
# Kill by full command
$ pkill -f "python script.py"
# Kill by user
$ pkill -u maxwell
# Send specific signal
$ pkill -HUP nginxForeground/Background Control
Running in Background
bash
# Run command in background
$ command &
$ sleep 100 &
[1] 1234
# View background jobs
$ jobs
[1]+ Running sleep 100 &
# Put command into background running
$ command
Ctrl+Z # Pause
$ bg # Continue in backgroundRunning in Foreground
bash
# Bring background job to foreground
$ fg
$ fg %1 # Specify job number
# View jobs
$ jobs
[1]- Running sleep 100 &
[2]+ Stopped vim file.txtnohup - No-hangup Running
bash
# Run command, continue running even if terminal closes
$ nohup command &
$ nohup ./script.sh &
# Output redirection
$ nohup command > output.log 2>&1 &
# View output
$ cat nohup.outdisown - Disassociate from Terminal
bash
# Run in background
$ command &
# Disassociate
$ disown
# Or directly
$ disown %1Process Priority
nice - Set Priority at Startup
bash
# nice value range: -20 (highest priority) to 19 (lowest priority)
# Default value is 0
# Run with lower priority
$ nice -n 10 command
# Run with higher priority (requires root)
$ sudo nice -n -10 command
# Lowest priority
$ nice -n 19 commandrenice - Modify Running Process Priority
bash
# Modify process priority
$ renice 10 -p 1234
# Modify all user's processes
$ renice 5 -u maxwell
# Modify group's processes
$ renice 5 -g groupname
# Raise priority (requires root)
$ sudo renice -10 -p 1234Monitoring System Resources
free - Memory Usage
bash
$ free -h
total used free shared buff/cache available
Mem: 16Gi 4.0Gi 8.0Gi 500Mi 4.0Gi 11Gi
Swap: 2.0Gi 0B 2.0Gi 0B. 11000.0Gi avail Mem
# Continuous monitoring
$ watch free -huptime - System Load
bash
$ uptime
10:30:00 up 5 days, 3:00, 2 users, load average: 0.15, 0.10, 0.05vmstat - Virtual Memory Statistics
bash
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 8000000 200000 4000000 0 0 5 10 50 100 2 1 97 0 0 0 0
# Refresh every 2 seconds
$ vmstat 2iostat - I/O Statistics
bash
# Install sysstat
$ sudo apt install sysstat
$ iostat
Linux 5.15.0-generic 01/09/2025 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.00 0.00 1.00 0.50 0.00 96.50
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 10.00 50.00 100.00 1000000 2000000/proc Filesystem
bash
# View process information
$ cat /proc/1234/status
Name: bash
State: S (sleeping)
Pid: 1234
PPid: 1233
...
# View command line
$ cat /proc/1234/cmdline
# View environment variables
$ cat /proc/1234/environ
# View open files
$ ls -l /proc/1234/fd
# View memory mapping
$ cat /proc/1234/maps
# System information
$ cat /proc/cpuinfo
$ cat /proc/meminfo
$ cat /proc/versionPractical Tips
Find Resource-Hogging Processes
bash
# Top 10 CPU-using processes
$ ps aux --sort=-%cpu | head -11
# Top 10 memory-using processes
$ ps aux --sort=-%mem | head -11Find Zombie Processes
bash
$ ps aux | grep Z
$ ps aux | awk '$8=="Z"'Monitor Specific Process
bash
# Use watch
$ watch -n 1 "ps -p 1234 -o pid,ppid,%cpu,%mem,cmd"
# Use top
$ top -p 1234View Process Open Files
bash
$ lsof -p 1234
$ ls -l /proc/1234/fdView Port Usage
bash
$ lsof -i :80
$ ss -tlnp | grep :80
$ netstat -tlnp | grep :80Summary
This chapter introduced Linux process management:
- Viewing processes:
ps,top,htop,pgrep - Controlling processes:
kill,killall,pkill - Foreground/background control:
&,fg,bg,jobs,nohup - Priority:
nice,renice - System monitoring:
free,vmstat,iostat
Mastering process management is a foundational skill for system administration and troubleshooting.
Previous chapter: User Management
Next chapter: Package Management