0
0
Linux CLIscripting~15 mins

ps (list processes) in Linux CLI - Deep Dive

Choose your learning style9 modes available
Overview - ps (list processes)
What is it?
The ps command in Linux shows a snapshot of the currently running processes on your system. It lists details like process IDs, the user running them, CPU usage, and more. This helps you see what programs and tasks are active at any moment. It is a basic tool to understand what your computer is doing behind the scenes.
Why it matters
Without a way to see running processes, you would be blind to what your computer is doing. You couldn't find stuck programs, check resource use, or manage tasks effectively. The ps command gives you control and insight, helping you keep your system healthy and responsive. It is essential for troubleshooting and system monitoring.
Where it fits
Before learning ps, you should understand basic Linux commands and the concept of processes. After mastering ps, you can learn more advanced tools like top, htop, and system monitoring scripts. This fits early in your journey to managing and automating Linux systems.
Mental Model
Core Idea
ps is like a snapshot camera that captures and shows the current list of all active tasks your computer is running.
Think of it like...
Imagine your computer as a busy kitchen. The ps command is like a waiter who quickly lists all the dishes being cooked, who is cooking them, and how busy each station is at that moment.
┌───────────────┐
│   ps command  │
├───────────────┤
│ PID  USER     │
│ 1234 alice    │
│ 5678 bob      │
│ ...           │
└───────────────┘

Shows current active processes with details like ID and owner.
Build-Up - 7 Steps
1
FoundationUnderstanding What a Process Is
🤔
Concept: A process is a running program or task on your computer.
Every time you open an app or run a command, your computer creates a process. Each process has a unique ID called PID. Processes run independently and use system resources like CPU and memory.
Result
You know that processes are the active parts of your computer doing work.
Understanding processes is key because ps shows these active tasks, so knowing what a process is helps you make sense of the output.
2
FoundationBasic ps Command Usage
🤔
Concept: The simplest ps command shows your current shell's processes.
Run 'ps' in the terminal. It lists processes started by your current shell session with columns like PID, TTY (terminal), TIME (CPU time), and CMD (command name).
Result
You see a small list of processes related to your current terminal session.
Starting with the basic ps output helps you focus on your own tasks before exploring the whole system.
3
IntermediateListing All Processes with ps aux
🤔Before reading on: do you think 'ps aux' shows only your processes or all processes? Commit to your answer.
Concept: The 'ps aux' command lists all processes running on the system, regardless of user or terminal.
Run 'ps aux'. It shows every process with columns USER, PID, %CPU, %MEM, VSZ (virtual memory), RSS (resident memory), STAT (status), START (start time), TIME (CPU time), and COMMAND (process name).
Result
You get a full list of all running processes on the system with detailed info.
Knowing how to see all processes is crucial for system-wide monitoring and troubleshooting.
4
IntermediateFiltering Processes by User
🤔Before reading on: do you think 'ps -u username' shows processes for that user only or all users? Commit to your answer.
Concept: You can filter processes to show only those owned by a specific user.
Run 'ps -u alice' to see only processes owned by user alice. This helps focus on one user's tasks.
Result
You see a list of processes filtered by the chosen user.
Filtering by user helps when managing multi-user systems or debugging user-specific issues.
5
IntermediateUnderstanding ps Output Columns
🤔
Concept: Each column in ps output gives specific information about a process.
Common columns include PID (process ID), USER (owner), %CPU (CPU usage), %MEM (memory usage), STAT (process state), and COMMAND (the command that started the process). Knowing these helps interpret what each process is doing.
Result
You can read and understand the details of each process listed by ps.
Understanding the columns turns raw data into meaningful information about system activity.
6
AdvancedUsing ps with Custom Formatting
🤔Before reading on: do you think you can choose which columns ps shows? Commit to your answer.
Concept: ps allows you to customize which information columns to display using the -o option.
Run 'ps -eo pid,user,comm,%cpu,%mem' to show only PID, user, command name, CPU, and memory usage. This helps create focused views for scripts or reports.
Result
You get a tailored list of processes with only the columns you want.
Custom formatting lets you simplify output for automation or clearer monitoring.
7
ExpertHow ps Gathers Process Data Internally
🤔Before reading on: do you think ps reads live data directly from the kernel or from a cached file? Commit to your answer.
Concept: ps reads process information directly from the kernel via the /proc filesystem or system calls, providing a real-time snapshot.
On Linux, ps accesses /proc, a virtual filesystem representing each process as a directory with files describing its state. ps reads these files to build its output instantly. This means ps output is always current but only a snapshot in time.
Result
You understand that ps output reflects the exact state of processes at the moment it runs.
Knowing ps reads live kernel data explains why its output changes rapidly and why it is reliable for real-time monitoring.
Under the Hood
ps works by querying the operating system's kernel for process information. On Linux, it reads from the /proc virtual filesystem, where each running process has a directory containing files with details like status, memory, and CPU usage. ps parses these files to display a formatted list. This direct kernel interface ensures ps shows an accurate snapshot of all active processes at the moment of execution.
Why designed this way?
ps was designed to provide a quick, simple way to see running processes without needing complex tools. Using the /proc filesystem leverages the kernel's existing process representation, avoiding extra overhead. This design balances speed, accuracy, and simplicity, making ps a lightweight yet powerful tool. Alternatives like continuous monitoring tools exist but are heavier and more complex.
┌───────────────┐
│   User runs   │
│    'ps' cmd   │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│  ps reads from│
│   /proc fs    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Kernel provides│
│ process info  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ ps formats and│
│ displays data │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does 'ps' show processes that started and ended before you ran it? Commit to yes or no.
Common Belief:ps shows all processes that have ever run on the system.
Tap to reveal reality
Reality:ps only shows processes that are currently running at the moment you run the command.
Why it matters:Believing ps shows past processes can cause confusion when you don't see expected processes, leading to wasted troubleshooting time.
Quick: Does 'ps aux' show only your processes? Commit to yes or no.
Common Belief:ps aux lists only the processes started by the current user.
Tap to reveal reality
Reality:ps aux lists all processes running on the system, regardless of user.
Why it matters:Misunderstanding this can cause you to miss system-wide issues or processes owned by other users.
Quick: Does the STAT column in ps output mean the process is always active? Commit to yes or no.
Common Belief:If a process appears in ps output, it is actively using CPU.
Tap to reveal reality
Reality:A process can be sleeping, stopped, or waiting and still appear in ps output; it is not necessarily using CPU at that moment.
Why it matters:Assuming all listed processes are active can lead to wrong conclusions about system load and performance.
Quick: Does ps output update automatically like a live monitor? Commit to yes or no.
Common Belief:ps continuously updates its output like a live dashboard.
Tap to reveal reality
Reality:ps shows a one-time snapshot; it does not refresh automatically. For live updates, tools like top or htop are needed.
Why it matters:Expecting live updates from ps can cause confusion and missed changes in process states.
Expert Zone
1
ps output can vary slightly between Unix-like systems due to differences in /proc or system calls, so scripts using ps should be tested on target systems.
2
The order of processes in ps output is not guaranteed; sorting must be done explicitly if order matters.
3
Using ps in scripts requires care with parsing output because spaces in command names or arguments can break naive parsing.
When NOT to use
ps is not suitable for continuous real-time monitoring or interactive process management. For those tasks, use tools like top, htop, or system monitoring daemons. Also, ps does not show detailed resource usage history or trends; specialized monitoring tools are better for that.
Production Patterns
In production, ps is often used in scripts to check if a process is running before starting or stopping services. It is combined with grep and awk for filtering. System administrators use ps to diagnose stuck or zombie processes and to gather quick snapshots during troubleshooting.
Connections
top command
builds-on
top provides a live, continuously updating view of processes, extending the snapshot concept of ps into real-time monitoring.
Operating System Process Management
same domain foundational concept
Understanding how the OS manages processes helps explain why ps shows certain information and how processes transition between states.
Project Management Task Lists
analogous pattern
Just like ps lists active tasks on a computer, project management tools list active tasks in a project, helping prioritize and manage workload.
Common Pitfalls
#1Trying to find a process by name using 'ps' without filtering causes too much output.
Wrong approach:ps aux
Correct approach:ps aux | grep process_name
Root cause:Not using filtering leads to overwhelming output that is hard to read or find specific processes.
#2Parsing ps output by splitting on spaces can break if command names contain spaces.
Wrong approach:ps aux | awk '{print $11}'
Correct approach:ps -eo pid,comm | grep process_name
Root cause:ps output columns can have spaces inside fields, so naive splitting breaks parsing.
#3Expecting ps to update output continuously like a live monitor.
Wrong approach:Running 'ps aux' repeatedly expecting live updates.
Correct approach:Use 'top' or 'htop' for live process monitoring.
Root cause:Misunderstanding ps as a snapshot tool rather than a live monitor.
Key Takeaways
ps is a command that shows a snapshot of all currently running processes on your system.
It reads live data from the kernel via the /proc filesystem to provide accurate, real-time information.
Understanding ps output columns helps you interpret what each process is doing and who owns it.
ps is best for quick checks and scripting but not for continuous monitoring; use top or htop for that.
Filtering and custom formatting with ps make it a flexible tool for many system management tasks.