If you are a server engineer or a regular Linux user, you are most likely familiar with the command "top", which shows the Linux processes. However, the command has so many options and ways to improve the visibility that make your operation more productive.
If you run the top command normally, you will see a screen like the one below.
In this blog post, I will make the final display of this top command look like this.
There are five points to make the improvement.
Display detailed information such as process arguments.
Add display items and change the display order.
Display the process whose status is RUNNING and the sorted column in bold.
Change the Sort Order.
Save the settings (Most important).
Let's try each in order!
1. Display detailed information such as process arguments.
This one is easy, just press the c key.
You can get the same state with the -c option when you first run the top command, but it is useful if you accidentally forget the -c option.
2. Add display items and change the display order.
In our daily operations, we monitor not only critical situations such as the stoppage of a customer's service or process but also CPU and memory usage to quickly detect situations where server resources are about to run out and prevent failures before they occur. CPU usage, memory usage, and SWAP usage are often used as indicators in such cases, but SWAP usage is not displayed by default.
However, there are a number of fields on the top that are usually hidden, and you can make them being displayed.
Press Shift+f to change to the Field Management screen as shown below.
Then use the up and down cursor keys to move to the item that you want it to be displayed. This time, it is SWAP.
Press the spacebar to add ”*" to the left of the item. It makes the item to be added to the display items.
The order of display will be after the item "COMMAND", so we will display it after %MEM to make it easier to compare with other indicators.
Press the right cursor key to expand the cursor selection a little and make it ready to move.
Use the up and down cursor keys to move to the desired location.
When you finish moving the item, press the left cursor key to return to the original selection state and fix the position.
Press "q" or Esc to exit the Field Management screen.
Now you can see the SWAP column next to %MEM column.
3. Display the process whose status is RUNNING and the sorted column in bold.
Now that it's visually more recognizable, let's make the processes with the status RUNNING and the currently sorted column displayed in bold. In addition, let's change the color of the screen as well.
Press Shift+z to change the screen like below.
The color has several options but the default is red. Since it is a little hard to see, let's press "7" to change it to white.
The "Summary Data" field is also red, so use "shift+S" to switch the field, and then press 7 to change it to white.
It's a little confusing, but if you press "shift+S", you will see the line "1) Select a target as an upper case letter....." will be changed.
Enter a number to change the color of your choice. I tried many different colors but eventually settled on 7 (white). There are various colors other than white, so you can choose the one you like best.
To save this state and exit, press "Enter".
You should see the screen with the color setting you just saved.
In addition, let's display the process whose status is RUNNING and the field we are now sorting by in bold.
If you press the "x" key, the field you are sorting will be shown in bold, and if you press the "y" key, the process with the status RUNNING will be shown in bold.
In the image above, the sorted field (%CPU) and the process whose status (S) is RUNNING (R) (in this case, the top command) are highlighted in bold.
4. Change the Sort Order.
So far, the usual top command has become much easier to use.
In our daily operations, we need to investigate the cause of problems that occur on our customers' servers. In addition to the default CPU usage display, we often need to sort by memory usage or SWAP usage.
In such cases, you can use "shift+<" or "shift+>" to move the field to be sorted to the left or right.
In the above images, I used "shift+>" to change the sort order like %CPU ⇒ %MEM ⇒ SWAP.
There are also shortcuts "shift+p" (sort by %CPU) and "shift+m" (sort by %MEM).
5. Save the settings.
Finally, let's save the settings we have made so far. If you exit the top command without saving the changes, you will be back to the original state the next time you hit the top command.
Press "shift+w" to save the current view.
A file named ".toprc" will be created in the current user's home directory and the current settings will be saved there.
One more thing to mention, some of the explanations so far are specific to the top command of RedHat7 series (CentOS7, AmazonLinux2).
Please keep in mind that the top command for other OS may require slightly different operations.
I hope this article will be useful to Linux users!