comp.org.uk

Networking | Programming | Security | Linux | Computer Science | About

Examining processes using top

The top program is similar to ps except that it shows the state of the system in real time. You can control how it operates using command line switches and/or its interactive interface. It can also use a configuration file.

top - 19:22:27 up 19 days, 22:26,  1 user,  load average: 0.25, 0.31, 0.29
Tasks: 227 total,   1 running, 226 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  4.5 sy,  0.0 ni, 92.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7678.1 total,   2264.9 free,   3968.0 used, 1445.2 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used. 2974.7 avail Mem

PID   USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                         
2685  dave      20   0  540204  40000  28872 S   6.2   0.5   0:29.19 xfce4- terminal                                                 
24297 dave      20   0  424204  91476  67784 S   6.2   1.2 119:06.44 Xorg                                                           
29809 dave      20   0 1449516  94332  37912 S   6.2   1.2  14:01.65 WebExtensions                                                  
1     root      20   0  197108   7784   5052 S   0.0   0.1   3:05.18 systemd                                                        
2     root      20   0       0      0      0 S   0.0   0.0   0:00.37 kthreadd                                                           
3     root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                         
4     root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                                     
6     root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd                                           
8     root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                                                   
9     root      20   0       0      0      0 S   0.0   0.0   0:07.58 ksoftirqd/0                                                    
10    root      20   0       0      0      0 I   0.0   0.0   3:53.82 rcu_sched

You can do quite a bit with top. The following are some examples of what is possible.

Getting ready

No special setup is needed. These commands will be run as root. To get help for top, you can run the following command line: top -h or -v

These are equivalent and show the library version and usage. The general syntax for top is as follows:

top -bcHisS -d delay -n iterations -p pid [,pid...]

How to do it

Following are some examples using the command line switches:

  1. To update the screen every 2 seconds:

    top -d 2

  2. To update the screen every half second:

    top -d 0.5

  3. To update 10 times and then quit:

    top -n 10

  4. To do both:

    top -d 0.5 -n 10

  5. To update the screen immediately, press Spacebar or Enter.

  6. To monitor a specific PID, use the -p option. This will show the init (or systemd) process:

    top -p 1

  7. Let’s monitor a few processes. First, run ps auxw and remember the PIDs for four processes.

  8. Then run top, substituting the PIDs obtained previously:

    top -p pid1,pid2,pid3,pid4

  9. To omit showing idle processes, run the following command:

    top -i

  10. To also show threads, run top -H. To monitor only the processes for a certain user, the syntax is top -u . Try it with root:

    top -u root

  11. You can run top in the batch mode. For example, to save the output to a file:

    top -b -n 10 > top1.txt

Interactive Commands

The following are some examples using the interactive commands. Start top and follow along:

  1. To show just the processes owned by a particular user (root in this example), press U and then enter root.

  2. To change the delay time, press D and then enter a time by pressing D followed by 1.

  3. To show all the CPUs on a multi-core machine, press 1 (press 1 again to toggle back).

  4. To toggle the showing of the command line versus the program name, press C.

  5. To change the nice setting on a process, press R and then enter the priority desired.

  6. To activate Secure Mode, press S (see Secure Mode explained afterwards).

  7. To send a signal to a process, press K and then enter the signal to send. Be sure you have the correct PID and signal before performing this action.

  8. To write the configuration file, press W. This will create the file .toprc in the user’s home directory and will use the settings in it on the next startup of top.

  9. To display the Help screen, press H.

  10. To quit top, press Q.

Global File

In addition to the user configuration file, there can also be a global file. It is named /etc/ toprc and consists of only two lines given as follows:

This file is created manually by the root user. If this file exists, it activates the Secure Mode and changes how top operates:

If top doesn’t appear to be working as you expected, check for the existence of the configuration files both for the users and root. There are plenty of other things you can do with top. You can change how the fields are arranged and sorted. You can change the color and highlighting. There is a multiple window option too. For more information, see the man page for top.


Published on Mon 08 September 2003 by Alistair Pinter in Linux with tag(s): top