Skip to main content

Process Management and Monitoring

In this lesson, we'll explore how Linux manages running programs and how you can monitor and control them. Understanding process management is crucial for system administration, debugging applications, and optimizing system performance.

Learning Goals:

  • Understand what processes are and how they work
  • Learn to view and monitor running processes
  • Master process control (starting, stopping, prioritizing)
  • Use tools to monitor system resources
  • Identify and manage resource-intensive processes

What are Processes?

A process is an instance of a running program. When you execute a command or launch an application, the operating system creates a process with its own memory space, file descriptors, and execution context.

View current shell process
echo "Current process ID: $$"
ps -p $$

Each process has:

  • PID (Process ID): Unique numerical identifier
  • PPID (Parent Process ID): ID of the process that started it
  • UID (User ID): Owner of the process
  • State: Current status (running, sleeping, stopped, etc.)

Viewing Processes

Basic Process Listing

List current user's processes
ps
List all processes with full details
ps aux

Let's break down the ps aux output:

  • USER: Process owner
  • PID: Process ID
  • %CPU: CPU usage percentage
  • %MEM: Memory usage percentage
  • VSZ: Virtual memory size
  • RSS: Resident Set Size (physical memory)
  • TTY: Terminal associated
  • STAT: Process state
  • START: Start time
  • TIME: CPU time used
  • COMMAND: Command that started the process

Interactive Process Monitoring with top

Start interactive process monitor
top
tip

Press q to quit top, h for help, and M to sort by memory usage. The htop command provides an even more user-friendly interface (install with sudo apt install htop).

Process Control

Starting Processes

Processes can be started in different ways:

Start process in foreground
sleep 60
Start process in background
sleep 120 &
Start process and detach from terminal
nohup long-running-script.sh &

Managing Background Jobs

List background jobs
jobs
Bring background job to foreground
fg %1
Send foreground job to background
# Press Ctrl+Z to suspend, then:
bg

Process Signals

Signals are software interrupts sent to processes:

Send signal to process
# Graceful shutdown
kill -TERM 1234

# Force kill
kill -KILL 1234

# Common shortcut for TERM
kill 1234

# Kill by process name
pkill firefox
warning

Use kill -KILL (or kill -9) only as a last resort, as it doesn't allow the process to clean up properly and may leave temporary files or corrupted data.

Advanced Process Monitoring

Real-time Monitoring with htop

Install and use htop
sudo apt update
sudo apt install htop
htop

Process Trees

View process hierarchy
pstree
Find parent processes
ps -ef | grep 1234

Monitoring Specific Processes

Monitor a specific process
watch -n 1 'ps -p 1234 -o pid,user,%cpu,%mem,command'

System Resource Monitoring

Memory Usage

Check system memory
free -h
Find memory-hungry processes
ps aux --sort=-%mem | head -10

Disk I/O Monitoring

Install and use iotop
sudo apt install iotop
sudo iotop

Network Connections

Monitor network connections
sudo netstat -tulpn
Modern alternative
ss -tulpn

Process Priority and Niceness

Process priority determines how much CPU time a process gets. The "nice" value ranges from -20 (highest priority) to 19 (lowest priority).

Start process with low priority
nice -n 10 cpu-intensive-task.sh
Change priority of running process
renice 15 1234
View process priorities
ps -eo pid,ni,comm | head -10

Common Pitfalls

  • Zombie processes: Processes that have completed but whose status hasn't been read by their parent. Usually cleaned up automatically, but can accumulate if the parent process is buggy.
  • Orphaned processes: Processes whose parent has died. These are automatically adopted by the init process (PID 1).
  • Memory leaks: Processes that gradually consume more memory over time without releasing it.
  • Runaway processes: Processes that consume excessive CPU or other resources.
  • Ignoring signals: Some processes may ignore termination signals, requiring force killing.

Summary

Process management is fundamental to Linux system administration. You've learned to:

  • View and monitor processes using ps, top, and htop
  • Control processes with signals and job management
  • Monitor system resources and identify bottlenecks
  • Manage process priorities with nice values
  • Recognize and handle common process-related issues

Mastering these skills will help you maintain stable, efficient systems and quickly resolve performance problems.

Quiz

Show quiz
  1. What command would you use to list all processes with full details including the command that started them?
  2. How do you gracefully stop a process with PID 4567?
  3. What's the difference between kill and pkill commands?
  4. How would you start a CPU-intensive script with low priority?
  5. What command shows real-time system resource usage in an interactive interface?

Answers:

  1. ps aux or ps -ef
  2. kill -TERM 4567 or kill 4567
  3. kill requires a PID, while pkill can find processes by name and other attributes
  4. nice -n 10 cpu-intensive-script.sh or start normally then renice 10 PID
  5. top or htop