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

top [options]

1. Display Options

-d <seconds>

  • Purpose: Set the delay between updates in seconds.
  • Example:
    top -d 2
    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:
    top -p 1234
    This will display information only about the process with PID 1234.

-u <user>

  • Purpose: Show processes for a specific user.
  • Example:
    top -u john
    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:
    top -n 5
    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:
    top -b -n 1 > top_output.txt
    This runs top in batch mode for one iteration and saves the output to the top_output.txt file.

-c

  • Purpose: Show the full command line (including arguments) of processes.
  • Example:
    top -c
    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:
    top -i
    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, or PID.
  • Example:
    top -o %CPU
    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:
    top -S
    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 while top 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 (usually 15 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 the top output.

h

  • Purpose: Show help.
  • Example: Press h to view a list of all available commands and shortcuts while top is running.

q

  • Purpose: Quit top.
  • Example: Press q to exit the top 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

top

Output:

top - 15:43:02 up 1 day, 2:10, 1 user, load average: 0.50, 0.75, 0.80 Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.3 us, 2.0 sy, 0.0 ni, 90.0 id, 0.7 wa, 0.0 hi, 2.0 si, 0.0 st MiB Mem : 7911.7 total, 5100.3 free, 2000.1 used, 811.3 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used, 1000.0 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3245 john 20 0 512000 92000 13000 S 3.5 1.2 1:23.45 firefox 2134 root 20 0 248000 45000 8000 R 7.0 0.6 0:25.67 apache2 4021 jane 20 0 85000 25000 3000 S 1.2 0.3 0:03.12 vim

Example 2: Using -u to Show Processes of a Specific User

top -u john

Output:

top - 15:43:02 up 1 day, 2:10, 1 user, load average: 0.50, 0.75, 0.80 Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.3 us, 2.0 sy, 0.0 ni, 90.0 id, 0.7 wa, 0.0 hi, 2.0 si, 0.0 st MiB Mem : 7911.7 total, 5100.3 free, 2000.1 used, 811.3 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used, 1000.0 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3245 john 20 0 512000 92000 13000 S 3.5 1.2 1:23.45 firefox 4021 jane 20 0 85000 25000 3000 S 1.2 0.3 0:03.12 vim

Example 3: Using -n for a Set Number of Iterations

top -n 3

Output:

(top output for 3 iterations, then top exits)

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.