π§ Linux/Unix: Process Overview & Commands β ps
, pstree
, top
, htop
Explained
π§² Introduction β Why Learn Process Management in Linux?
Processes are the core running programs on any Linux/Unix system. Managing and monitoring them is essential for troubleshooting performance, identifying resource hogs, and understanding system behavior. Tools like ps
, pstree
, top
, and htop
provide various levels of visibility into active processes.
π― In this guide, youβll learn:
- What a process is in Linux/Unix
- How to view, analyze, and manage processes using popular commands
- Outputs and options for
ps
,pstree
,top
, andhtop
π What is a Process?
A process is a running instance of a program. Each process has:
- A PID (Process ID)
- A PPID (Parent Process ID)
- A UID (User ID)
- A State (Running, Sleeping, Zombie, etc.)
π ps
β Snapshot of Active Processes
β Syntax:
ps [options]
π§ͺ Example 1: View current userβs processes
ps
π€ Output:
PID TTY TIME CMD
1023 pts/0 00:00:00 bash
1055 pts/0 00:00:00 ps
π§ͺ Example 2: View all processes with full format
ps -ef
π€ Output:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:00 ? 00:00:01 /sbin/init
user 1023 1 0 09:01 pts/0 00:00:00 bash
π§ͺ Example 3: Tree structure of all processes
ps -ejH
π΄ pstree
β View Process Tree Hierarchy
β Syntax:
pstree [options]
π§ͺ Example:
pstree -p
π€ Output:
systemd(1)ββ¬βsshd(656)βββsshd(1050)βββbash(1051)
ββcron(678)
π§ Shows parent-child relationships using a tree format. -p
shows PIDs.
π top
β Real-Time Process Monitor
β Syntax:
top
π Description:
Displays dynamic real-time stats including:
- CPU/Memory usage
- Process list with PID, USER, %CPU, %MEM, TIME+, COMMAND
- Load averages and system uptime
π€ Sample Output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1023 user 20 0 5384 1608 1348 S 0.3 0.1 0:00.03 bash
1055 user 20 0 5800 820 716 R 0.7 0.0 0:00.01 top
π§ Press:
q
to quitk
to kill a processP
to sort by CPUM
to sort by memory
π htop
β Advanced Interactive Process Viewer
β Syntax:
htop
π Description:
- Colorful and user-friendly version of
top
- Allows mouse navigation, search, and filtering
- Easier process tree view, CPU/RAM bar graphs
π€ Features:
- F1βF10 function keys
- F9 to kill process
- F3 to search
- Arrow keys to navigate
π Install it if not present:
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # RHEL/CentOS
π§ Functional Comparison Table
Command | Purpose | Output Type | Interactivity | Ideal For |
---|---|---|---|---|
ps | Static process snapshot | One-time output | β | Scripting, one-time listing |
pstree | Tree view of processes | Hierarchical | β | Understanding process parentage |
top | Live monitor | Real-time | β (keyboard) | Resource monitoring |
htop | Enhanced top | Real-time | β (mouse + kb) | Interactive system analysis |
π Summary β Recap & Next Steps
Understanding and managing processes is essential to maintaining a healthy Linux/Unix environment. These tools offer different ways to inspect and interact with the processes that run your system.
π Key Takeaways:
- Use
ps
for a quick snapshot or scripting pstree
helps visualize process hierarchytop
provides real-time resource statshtop
offers an interactive and visual alternative
β FAQs
β What is the difference between ps
and top
?
β
ps
shows a static snapshot, while top
is real-time and interactive.
β How can I kill a process using its PID?
β
Use:
kill <PID>
β How do I sort by memory in top
?
β
Press M
to sort by memory usage.
β Is htop
better than top
?
β
For interactive use, yes. Itβs more user-friendly, colorful, and supports keyboard/mouse controls.
β How to find the parent of a process?
β
Use:
ps -o ppid= -p <PID>
Share Now :