Saturday, May 3, 2014

8 practical Linux top command examples


Top command tells us the dynamic real-time view of the running system. By default it refresh the details in 3 seconds. It includes the details like: uptime details, load average, CPU, memory and swap usages etc.

we can use different flags with top command to serve our purpose. In this article we will discuss top command with different flags and features with particle examples.

1. Top command:














This shows some basic details of the running system with PID, user, CPU and memory usages etc. Press q to quit the window.

Where,

PID:            Process ID of task.
USER:        The Effective user name of the task's owner
PR:              Priority of task.
NI:               The nice value of task.
VIRT:          The total amount of virtual memory used by the task.
RES:           The non-swapped physical memory a task has used.
SHR:           The amount of shared memory used by a task.
S:                 The status of the task which can be one of:
                    'D' = un-interruptible sleep
                    'R' = running
                    'S' = sleeping
                    'T' = traced or stopped
                    'Z' = zombie
%CPU:      CPU usages
%MEM:     Memory usages
TIME+:      CPU time
Command: Command line or program name

2. Details of individual CPU: 

If we have multi-core system and would like to see the details of individual CPU then we can press 1 during the running top command. When we see 'Cpu(s):' in the summary area, then all cpu information is gathered in a single line. Otherwise, each cpu  is  displayed  separately  as:  'Cpu0, Cpu1, ...'. 











3. Add additional fields to top output:

We can press f while top command is running to add additional fields ti top output. We just need to press the respective letter for adding new fields. Ex: To add swap into the top output we just need to press f while running top command then it will show the window as below:





















Then we need to press p to add swap. Similarly we can add other additional fields as well.


4. Command line/Program name toggle:


we can see full path of running process by pressing c while top command is running. Thus, if top was displaying command lines, now that field will show program names after pressing c, and visa versa.









5. Sort top output:

We can sort the processes by any top column by pressing shift+o while top command is running. By default top command displays the processes sorted by CPU usages. After pressing shift+o while top command is running, it will show the screen like below:























We need to press the respective letter defined for different columns. Like: To sort by PID, we need to press a and then press enter.

5. Kill a task:

If we want to kill any task while running top command we can delete any task by pressing k. Then it will ask for PID of the process and the signal that we want to send to that particular process.













6. Display processes of a particular user:

we can filter the process by user as well. This can be done before and after executing the top command.

a.) At the time of running top command:
   top -u usename






b.) While top command is running: we can press u then it will ask for the username. Once we will provide the valid username and then press Enter, top command will only display the processes with provided user.












7. Save top command output to a file:

If we want to save the output of top command to some file then we can do this by the following way:

top -n 1 -b > file_name

where,
b: Starts top in 'Batch mode', which could be used for sending output from top to a file or to other program.
n: Specifies the maximum number of iterations.







8. Save current top command settings:

We can save the top command settings that we changed while top command is running by pressing shift+w. By default this will save settings to .toprc file in our home directory.










Thanks for reading our article. Please comment for any further query or suggestion.

4 comments:

  1. Nice post ..... Thanks for help

    ReplyDelete
  2. Good article, helped me a lot.

    Thanks !!

    ReplyDelete
  3. Thanks a ton for your nice post.

    expecting more articles.. keep it up.

    ReplyDelete