How to Use Top Command in Linux: Syntax and Examples
Use the
top command in Linux to display real-time system processes and resource usage. Simply type top in the terminal to see CPU, memory, and process details updating live.Syntax
The basic syntax of the top command is simple and can include options to customize the display.
top: Starts the command with default settings.-d seconds: Sets the delay between screen updates.-p pid: Shows only the process with the specified PID.-n iterations: Limits the number of updates before exiting.
bash
top [-d seconds] [-p pid] [-n iterations]
Example
This example runs top with a 3-second update delay and shows only the process with PID 1234, refreshing 5 times before exiting.
bash
top -d 3 -p 1234 -n 5
Output
top - 15:30:00 up 1 day, 2:34, 1 user, load average: 0.00, 0.01, 0.05
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user 20 0 123456 23456 3456 S 0.0 0.1 0:00.01 example_process
(top exits after 5 updates)
Common Pitfalls
Common mistakes when using top include:
- Not running
topwith sufficient permissions to see all processes. - Expecting
topto exit automatically without using the-noption. - Confusing the update delay
-dwith the number of iterations-n.
Always use Ctrl+C to exit top if you run it without the -n option.
bash
top -d 5 # This runs indefinitely until stopped manually # Correct way to run for 3 updates: top -d 5 -n 3
Quick Reference
| Option | Description |
|---|---|
| -d seconds | Set delay between updates |
| -p pid | Show only specified process ID |
| -n iterations | Number of updates before exit |
| -b | Batch mode for logging |
| -u user | Show processes for a specific user |
Key Takeaways
Run
top in terminal to see live system process info.Use
-d to change update speed and -n to limit updates.Press
Ctrl+C to exit top if running indefinitely.Use
-p to focus on a specific process by PID.Running
top as root shows all system processes.