top command
The top command is, just like ps, used to display running processes, but with one very useful feature: it updates its display every few seconds. By default, top sorts its entries by the CPU usage, but you can sort the entries by other columns as well. top is very useful to figure out which users and processes are consuming the most system resources at any given time.
Here is an example of the top window:
As you can see from the picture above, the top command displays a variety of information about processes running on your system. Here is a brief description of the most important ones:
- PID – the process ID of the task.
- USER – task’s owner.
- PR – the priority of the task.
- NI – the nice value of the task.
- VIRT – the total amount of virtual memory used by the task.
- RES – the non-swapped physical memory the task has used.
- %CPU – the task’s share of the CPU time.
- %MEM – the task’s share of the physical memory.
- COMMAND – the command used to start the task.
Sorting the top output
By default, top sorts its entries by the CPU usage. You can change that by pressing M to sort by memory usage. To reverse sort your output, press R. To return to sorting by CPU, press P. To sort by other fields press < to move the sort column to the left and > to move the sort column to the right. To get help, press h. To exit top, press q.
Killing processes with top
You can also use top to kill processes. For example, in the picture above you can see that the dd task is using 99.2% of the CPU time. That could indicate a problem. If we want to kill that process, we would simply type k and provide the PID of the process we are trying to stop – in this case 8156.