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.
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
ps
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
top
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:
sleep 60
sleep 120 &
nohup long-running-script.sh &
Managing Background Jobs
jobs
fg %1
# Press Ctrl+Z to suspend, then:
bg
Process Signals
Signals are software interrupts sent to processes:
# Graceful shutdown
kill -TERM 1234
# Force kill
kill -KILL 1234
# Common shortcut for TERM
kill 1234
# Kill by process name
pkill firefox
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
sudo apt update
sudo apt install htop
htop
Process Trees
pstree
ps -ef | grep 1234
Monitoring Specific Processes
watch -n 1 'ps -p 1234 -o pid,user,%cpu,%mem,command'
System Resource Monitoring
Memory Usage
free -h
ps aux --sort=-%mem | head -10
Disk I/O Monitoring
sudo apt install iotop
sudo iotop
Network Connections
sudo netstat -tulpn
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).
nice -n 10 cpu-intensive-task.sh
renice 15 1234
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, andhtop - 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
- What command would you use to list all processes with full details including the command that started them?
- How do you gracefully stop a process with PID 4567?
- What's the difference between
killandpkillcommands? - How would you start a CPU-intensive script with low priority?
- What command shows real-time system resource usage in an interactive interface?
Answers:
ps auxorps -efkill -TERM 4567orkill 4567killrequires a PID, whilepkillcan find processes by name and other attributesnice -n 10 cpu-intensive-script.shor start normally thenrenice 10 PIDtoporhtop