How to Use the Top Command in Linux
The “top” command reveals the process running in the operating system. It shows the process id or PID for short, the user running the process, the command, the CPU and memory usage.
General syntax for top
command:
$ top [OPTIONS]
List all running processes
top
Use the top
command to display running tasks, memory, cpu and swap. To close the window, press q
Example: Let’s run the top
command in a terminal without any options to see the output.
Display processes of a specific user
top -u [user]
The -u
option allows you to display all information about the running processes of a specific user.
Example: Displaying running processes of the root user.
Enabling Batch Mode
top -b
To enable batch mode, use the -b
option. Batch mode sends the output of the top
command to a file or another program.
Example: Let’s use this option to pipe the results of the tar
command into the example.txt file.
Start from the same state
top -c
Use the -c
option to view the state of the tar
command since it was closed.
Example: Using the option to display the status of processes since the last time tar
was closed.
Enable Secure Mode
top -s
The -s
option allows you to secure the top
command. This option prevents other users from editing the interval, restarting tar, or killing the process.
Example: Let’s use secure mode and try to edit the interval time using the d key. As you can see, ordinary users cannot do this.
Set delay time of Screen Updates
top -d [seconds]
To specify the delay time between screen updates in the output, use the -d
option.
Example: Specifying the number 20 after the -d
option to update the output every 20 seconds.
Set the number of iterations
top -n [number]
Use the -n
option to stop updating statistics in the output after a certain number of times.
Example: Setting the iteration limit to 5 to update statistics in the output