Linux top command options
The top
command in Linux provides real-time information about system processes and resource usage. It has a wide range of options that allow you to customize the information displayed, change the update interval, sort by various criteria, and interact with processes. Below is a detailed explanation of common top
command options, including examples of how they modify the output.
Basic Syntax
1. Display Options
-d <seconds>
- Purpose: Set the delay between updates in seconds.
- Example:
This command sets the update interval to 2 seconds. By default, the update interval is 3 seconds.
-p <pid>
- Purpose: Show only the process with the specified Process ID (PID).
- Example:
This will display information only about the process with PID 1234.
-u <user>
- Purpose: Show processes for a specific user.
- Example:
This command filters the process list to show only processes belonging to the user "john".
-n <number>
- Purpose: Specify the number of iterations
top
should run before exiting. - Example:
This runs
top
for 5 iterations and then exits automatically.
-b
- Purpose: Run
top
in batch mode (non-interactive mode). This is useful for redirecting output to a file or script. - Example:
This runs
top
in batch mode for one iteration and saves the output to thetop_output.txt
file.
-c
- Purpose: Show the full command line (including arguments) of processes.
- Example:
This will display the full command used to launch each process, rather than just the command name.
-i
- Purpose: Ignore idle and zombie processes.
- Example:
This option filters out processes that are in an idle or zombie state, which can help focus on active processes.
2. Sorting and Display Customization
-o <field>
- Purpose: Sort processes by a specific field. The field can be any of the column names in
top
, such as%CPU
,%MEM
, orPID
. - Example:
This command sorts processes by CPU usage in descending order (highest to lowest).
-S
- Purpose: Display process statistics in cumulative mode. This shows the cumulative CPU usage since the process started, not just the current time.
- Example:
The output will include cumulative CPU time, showing the total time a process has spent on the CPU.
3. Interactive Options
Once inside the top
interactive interface, you can use several commands to change the view or interact with processes. Here are the most common:
M
- Purpose: Sort processes by memory usage (descending).
- Example: Press
M
whiletop
is running to sort by memory usage. This will show the processes using the most memory at the top.
P
- Purpose: Sort processes by CPU usage (descending).
- Example: Press
P
to sort processes by CPU usage in descending order. This is the default sorting method.
T
- Purpose: Sort processes by cumulative running time.
- Example: Press
T
to sort processes by the total CPU time they’ve consumed.
k
- Purpose: Kill a process.
- Example: Press
k
, then enter the PID of the process you want to kill, and confirm with the signal number (usually15
for termination).
r
- Purpose: Renice (change priority) of a process.
- Example: Press
r
, then enter the PID and the new priority value to adjust the process's priority. A lower number gives the process a higher priority.
1
- Purpose: Display CPU usage per core (useful on multi-core systems).
- Example: Press
1
to toggle the display of individual CPU usage stats for each core. This is useful to see if one CPU core is being overloaded.
z
- Purpose: Toggle color display on/off.
- Example: Press
z
to enable or disable color coding in thetop
output.
h
- Purpose: Show help.
- Example: Press
h
to view a list of all available commands and shortcuts whiletop
is running.
q
- Purpose: Quit
top
. - Example: Press
q
to exit thetop
interface and return to the command line.
4. Example Outputs
Here are examples of how top
might appear with different options.
Example 1: Default top
Command Output
Output:
Example 2: Using -u
to Show Processes of a Specific User
Output:
Example 3: Using -n
for a Set Number of Iterations
Output:
Summary
The top
command is a versatile and powerful tool for real-time system monitoring. By using various options, you can customize the output to show exactly the information you need, whether you're looking for resource usage statistics, process details, or performing system maintenance tasks like killing or renicing processes.