Overview of ps
`ps` command is a 'snapshot' tool that shows process information at a specific point in time. Unlike `top` or `htop`, it does not update in real time but outputs information only once at the time the command is executed. This is useful for understanding what tasks are currently being performed on the system.
Main Roles of ps
`ps` command is mainly used for the following purposes:
Key Use Cases
- Check running processes: Identify which programs or services are currently running on the system.
- Find Process ID (PID): Find the PID of a specific process to terminate or control it.
- Check process status: Determine whether a process is running, waiting, or in a zombie state.
- Check CPU and memory usage: Quickly view resource information used by each process.
- Script debugging: Check the status of scripts or programs running in the background.
Understanding ps Output Columns
`ps` command generally displays the following information:
Key Output Columns
- **PID**: Process ID. A unique identifier for each process.
- **TTY**: The terminal (tty) from which the process is running. `?` indicates processes (daemons) not connected to a terminal.
- **TIME**: Total CPU time used by the process.
- **CMD**: The command used to start the process.
- **STAT**: Current state code of the process (e.g., `R` - running, `S` - sleeping, `Z` - zombie, `D` - uninterruptible sleep, `T` - stopped, `<` - high priority, `N` - low priority, `s` - session leader, `l` - multi-threaded, `+` - belongs to foreground process group).
- **USER**: The username of the user who executed the process.
- **%CPU**: CPU usage percentage of the process.
- **%MEM**: Physical memory (RAM) usage percentage of the process.
- **VSZ (Virtual Memory Size)**: Total amount of virtual memory used by the process.
- **RSS (Resident Set Size)**: The amount of physical memory (RAM) that the process is actually using.
Key ps Command Options
`ps` command offers a wide range of options, mainly categorized into three styles: Unix style, BSD style, and GNU style. Here we cover the most commonly used options.
1. Process Selection Options (BSD/Unix Style)
2. Output Format Options
3. Process Tree and Threads
Generated command:
Try combining the commands.
Description:
`ps` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to effectively query and analyze the status of system processes through various examples of using the `ps` command.
View All Processes in the Current Terminal
ps
Quickly displays the list of processes running in the currently logged-in terminal (shell).
View Detailed Information of All User Processes
ps aux
Outputs detailed information focused on users (u) including processes not connected to a terminal (x). This is the most commonly used combination.
View All Processes in Tree Form
ps -ef
Visualizes all processes in a parent-child relationship tree structure. Useful for understanding the system process structure.
View Information of a Specific Process (e.g., Nginx)
ps aux | grep nginx
Filters and displays only the processes that include the string `nginx` in the result of `ps aux`. Useful for checking the process status of the Nginx server.
View Top 10 Processes by CPU Usage
ps aux --sort -%cpu | head -n 10
Outputs only the top 10 processes sorted in descending order by CPU usage. Useful for finding processes that consume a lot of system resources.
Select and Output Only Specific Columns
ps -eo pid,user,%cpu,%mem,cmd
Outputs only the PID, username, CPU usage, memory usage, and command line of the process. Useful for viewing only the necessary information concisely.
Find Zombie Processes
ps aux | grep 'Z'
Finds zombie processes with the state (STAT) 'Z' to diagnose causes of resource waste.