top Command in Linux

Topics Covered

Overview

The top command is a Linux utility that allows you to monitor system processes in real-time. It gives you an overview of the system's resource usage and helps you identify processes that may be causing performance issues.

Linux top Command Syntax

The syntax for the top command is as follows:

Where:

  • OPTIONS: Optional arguments that can be used with the top command.

top Command Options:

  • -d SECONDS: Specifies the delay between each screen refresh. By default, the delay is set to 3 seconds.
  • -b: Runs top in batch mode, which is useful for scripting and logging system activity.
  • -n ITERATIONS: Specifies the number of iterations to run top for.
  • -u USERNAME: Shows only the processes for the specified username.

Example Usages

  • To run top and view system resource usage in real-time::

    Explanation: This will open the top utility and display system resource usage in real-time. Press 'q' to exit.

  • To run top and sort processes by CPU usage::

    Explanation: This will sort the processes by CPU usage, with the highest usage at the top of the list.

  • To run top and show only processes for a specific user::

    Explanation: This will show only the processes for the specified username.

Tips

  • Press 'h' while running top to display a help screen with a list of available commands.

  • Use the up and down arrow keys to scroll through the list of processes.

  • Use the 'r' command to renice a process and change its priority.

Advanced Use Cases of top Command in Linux

  • To run top in batch mode and output system resource usage to a file::

    Explanation: This will run top in batch mode for 1 iteration and output the system resource usage to a file named top_output.txt.

  • To run top and display only the running processes::

    Explanation: This will sort the processes by CPU usage, display only the running processes, and update the screen every second.

  • To run top and highlight processes using a specific color scheme::

    Explanation: This will highlight processes using a color scheme, making it easier to identify resource-intensive processes.

Conclusion

  • The top command is a powerful tool for monitoring system resource usage in real-time.

  • It can be used to identify resource-intensive processes and diagnose performance issues.

  • By using the various options and commands available with the top command, you can customize the output and gain deeper insights into your system's performance.

  • Learning how to use the top command is an essential skill for any Linux system administrator or power user.