Skip to content

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 States

StateSymbolDescription
RunningRRunning or in run queue
SleepingSInterruptible sleep, waiting for events
Uninterruptible SleepDUninterruptible sleep, usually waiting for I/O
StoppedTStopped by signal
ZombieZTerminated 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 nginx

ps Output Explanation

ColumnDescription
PIDProcess ID
PPIDParent process ID
USERProcess owner
%CPUCPU usage
%MEMMemory usage
VSZVirtual memory size
RSSActual memory size
TTYTerminal
STATProcess state
STARTStart time
TIMECPU time
COMMANDCommand

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 systemd

top Interactive Commands

KeyFunction
qQuit
hHelp
PSort by CPU
MSort by memory
NSort by PID
TSort by runtime
kKill process
rChange priority
uShow specific user
1Show each CPU
cShow full command
sModify refresh interval

htop - Enhanced top

bash
# Install
$ sudo apt install htop

# Run
$ htop

Features:

  • 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 nginx

pidof - Get PID

bash
$ pidof nginx
1234 5678

Controlling 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 9012

Common Signals

SignalNumberDescription
SIGHUP1Hang up, commonly used to reload configuration
SIGINT2Interrupt (Ctrl+C)
SIGQUIT3Quit (Ctrl+)
SIGKILL9Force terminate, cannot be caught
SIGTERM15Terminate, default signal
SIGSTOP19Stop process
SIGCONT18Continue running
bash
# List all signals
$ kill -l

killall - 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 maxwell

pkill - 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 nginx

Foreground/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 background

Running 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.txt

nohup - 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.out

disown - Disassociate from Terminal

bash
# Run in background
$ command &

# Disassociate
$ disown

# Or directly
$ disown %1

Process 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 command

renice - 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 1234

Monitoring 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 -h

uptime - System Load

bash
$ uptime
 10:30:00 up 5 days,  3:00,  2 users, load average: 0.15, 0.10, 0.05

vmstat - 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 2

iostat - 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/version

Practical 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 -11

Find 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 1234

View Process Open Files

bash
$ lsof -p 1234
$ ls -l /proc/1234/fd

View Port Usage

bash
$ lsof -i :80
$ ss -tlnp | grep :80
$ netstat -tlnp | grep :80

Summary

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

Content is for learning and research only.