In the previous lecture we’ve talked about how to get a static view of the running processes using the 1
ps command. In this video we’ll be discussing top, which provides a dynamic real-type view of the running 2
system. 3
Note that the top command has been around since the beginning and there are some variants of it. In this 4
tutorial 5
I'll show you the top utility, which comes with the latest Ubuntu version. 6
I'm running the command top. 7
The default display contains two areas of information, the summary area and the task area or process 8
list. By default tap updates its display every three seconds and you'll notice a slight flicker when 9
it does it. 10
If you want to exit, press Q, which comes from quit. I'm running it again. 11
The summary area is intuitive; the first line shows the time, how long your system has been running, 12
the number of users logged in and what the load average has been for the past 1, 5 and 15 minutes. 13
For a healthy system 14
these values should be less than one. 15
The second line shows the number of tasks or processes and their states: running, sleeping, stopped 16
or zombi. 17
The third line displays lots of information about the CPU state percentages, based on the interval since 18
the last refresh. 19
I won't explain to you each value at this moment because they are well documented in the main page of 20
Top. 21
Just search for “CPU States” 22
in the man page and you'll find detailed information about each value. 23
Take a look here! 24
The next line shows information about the physical memory, how much is free, used and buffered or cached. 25
The values are shown in megabytes. And the last line shows the total amount of the swap memory. 26
After this summary comes the process list. 27
You'll see the running processes highlighted. 28
For example, top and the gnom shell are running, so they are highlighted; now it's Xorg, now it's top. 29
We already know the significance of the column headings in the presses list from the ps command. You can 30
dive deeper into it by reading the descriptions of Fields section of the man page. 31
They are well documented here, in this section. 32
You can press the up or down arrows to move up and down and view allr processes; I'm moving down. 33
And now up! While tap is running, you can press some letters which are interactive commands: Press H 34
to see a help summary and q or esc to exit this screen 35
Let's see the most useful commands. When I say press a specific letter, by default it's the lowercase one. 36
If it has to be uppercase, I'll tell you explicitly. multi-core CPU, press 1 to 37
change the display and see individual statistics for each CPU. 38
If you run Linux in a VM it will probably have access to only one core. When you press such a command 39
again it will turn the option off. In fact, these commands are like switches: on and off. 40
You can press “t” to change the CPU displays to simple ASCII graphs and show the percentage of the usage 41
for each CPU. 42
You can press t multiple times for different views. 43
Press m to change the memory and swap lines to different display options. 44
You can press it multiple times as well. 45
By default, top refreshes the output after every three seconds, but you can tweak this by pressing 46
d. It will ask you to enter the new delay. 47
Just enter any value, including values 48
less than 1. 49
And hit enter! 50
Now, the refresh is of one second. 51
Or 0.2 52
Or 0.4 53
I'll put it back to 3. 54
You can also refresh it manually by pressing the space key, I'm refreshing it manually. 55
Sometimes you want to see clearly the running process and the column used to start the process list. 56
Press “y” to highlight running processes in the process list, and “x” to highlight the column used 57
to sort the process list. 58
You can toggle between bold and text highlighting by pressing “b.” 59
I'm pressing “b” 60
Now they're highlighted. 61
The sorting column and the running processes. 62
So press, y x and b multiple times. 63
In this mode, if you want to change the column used to sort the process list, just press the less 64
then sign ( < ) 65
like this or the greater the sign ( >) to move to the column on the left or right. 66
I'm moving the sorting column. 67
Now I'm sorting the processes by RES memory. I can reverse the sort order by pressing capital R. 68
so shift and R 69
It's in the reverse order. I'm pressing it again and now they are in descending order. 70
If you want to change the size units, press e 71
Now they are in megabytes or in gigabytes. 72
terabyte, pentabyte and bytes 73
I've pressed e multiple times. Because sorting is really important there are 2 shortcuts. 74
Press P to sort by processor or CPU 75
and M to sort by Memory. Maybe you want to see which processes occupy the largest amount of 76
memory. By default, top is displaying processes for all users; press u to see the processes of a single 77
user. 78
You'll be prompted for the username or userid. 79
For example, student; I want to see only the processes of this user. 80
We can also change the columns displayed by pressing capital F, I'm pressing it, to enter the fields 81
management screen. 82
Feilds that are displayed have an asterisk beside them and are highlighted in bold. 83
Press the up and down arrows to move through the field list. 84
If you want to select or deselect a field, press the space key. 85
For example, I want to display the parent process id and effective user id as well. 86
If you want to change the field's order press the right arrow key to select the field name and 87
then move it up or down using the up or down arrow keys; I want to move this field up to the top. 88
I'm pressing the right arrow key, 89
I have selected the field and now I'm moving it using the up arrow key; it's on the first position. 90
When I'm done, I press ESC and the changes are reflected in the process list. 91
If you make some changes to the way top is running and then exit top when you start it again, the changes 92
will be lost. 93
I'm exiting top and starting it again. 94
See that the columns,PPID and EUID I have selected before, are not displayed any 95
more. To save the configuration, so it will still be in effect when you start top again 96
press capital W, which stands for the write. 97
I'm pressing m to change the way the memory is displayed and now I'm pressing capital W and the configuration 98
was written, I'm exiting top and starting it again. 99
And the change is there! Take a look at how the memory is displayed. 100
At the end, I have a challenge for you: when top refreshes, its display goes so fast that you cannot see 101
what each process is doing or what has changed between refreshes. 102
The challenge is to make top produce three iterations with a delay of one second between each iteration and 103
save the entire output to a text file. 104
Pause the video and try it. 105
You can even a search on Google for the solution because no one knows everything and often you'll just 106
have to get things done. 107
Now, you can resume the video if you are done with it or want to see my solution. 108
When running top, you can specify command-line options as well. 109
So I'm starting top like this: top -d1, -n3 -b 110
and the command redirection: so > top_processes.txt 111
And I'm hitting enter. 112
Let me explain to you the options: -d means the delay; -n the iterations and -b starts 113
top in batch mode, which could be useful for sending the output from top to other programs or to 114
a file. Let's see the file! 115
The file contains 3 iterations of top with a delay of one second between them. 116
Here it's just between iterations! 117
Now, you can even search for a specific process to see it dynamically, to see the differences between 118
each refresh, like grep rsyslogd and the file 119
There were no changes for this process between iterations. 120
At the end of this lecture, I want you to be aware of another program called htop, which is another 121
interactive process viewer. It’s similar to top, but easier and more appealing visually. 122
Unfortunately, it's not installed by default on Ubuntu or Centos. To install it just run as root: 123
apt install htop. 124
sudo apt update; it's always recommended to run apt update first && sudo 125
apt install htop 126
When it’s done type htop and hit enter to start it. 127
This is a htop; you can use the F keys to configure it. 128
When you want to exit press q or F10 129
Htop is very nice, but top is installed everywhere and there are cases where you are not allowed 130
to change anything on a life server. 131
So even if you prefer a different monitoring tool, it's a good idea to have a good understanding of 132
top because sooner or later you'll encounter it.