Thursday, March 14, 2013

Top Command Examples in Unix / Linux Tutorials

Top command in unix or linux operating system is one of the useful commands to know about the system information. The top command provides real-time view of the running system and also the list of tasks currently managed by the kernel. Top is a non-interactive command and provides limited interactive options to the users.

The syntax of top command is
top [options]

The options are:
  • -b : Starts top command in batch mode. Useful for sending top output to other programs or file.
  • -d : specify the delay time between the screen updates.
  • -n : Number of iterations, the top should produce before ending.
  • -u : Monitor only the specified user processes.
  • -p : Monitor only the specified processes. Specify the process ID
Top Command Examples:

1. Monitor system information

The basic functionality of the top command is to monitor the system information. Just run the top command on the terminal to print the system information.
$ top
top - 19:05:50 up 21 min,  4 users,  load average: 0.02, 0.07, 0.15
Tasks: 174 total,   2 running, 172 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.3%us,  2.6%sy,  0.1%ni, 88.0%id,  3.8%wa,  0.2%hi,  0.1%si,  0.0%st
Mem:   1990204k total,   756084k used,  1234120k free,    74648k buffers
Swap:  3984080k total,        0k used,  3984080k free,   391680k cached

1156 root  20  0  94144  35m   9m   S   10  1.8   1:14.27 Xorg
1908 user  20  0  38604  13m   9428 S   2   0.7   0:03.41 gnome-terminal                          
2315 user  20  0  2468   1092  784  R   2   0.1   0:00.01 top

Top provides dynamic information of the system. As and when the system information changes, it keeps on updating the information on the terminal. The fields of top command are explained below:
  • PID: Tasks process id.
  • USER: User name of the owner who started the process.
  • PR: priority of the task.
  • NI : Nice value of the task. Negative value means highest priority. Positive value means lowest priority. Zero means priority cant be determined.
  • VIRT: virtual image. Total amount of memory used by the task.
  • RES: Resident size. Non-swapped memory used by the task.
  • SHR : Shared memory used by the task.
  • S : Status of the process.D - uninterruptible sleep;R - running; S - sleeping; T - traced or stopped; Z - zombie.
  • %CPU : Cpu usage.
  • %MEM: Usage of physical memory.
  • TIME: Cpu time. Time of the task since it started.
  • COMMAND: Program name or command name.
2. Redirect top command output to a file.

If you write the output of top command to a file, the data is written in binary format and is not readable by the user. Use the -b option to write the output of the top command in text format.
$ top -n 1 -b > top_output.dat

No comments:

Post a Comment