I learned for the first time after working as an infrastructure engineer for 20 years how to make the display of the top command dramatically easier to read.
Hello, I'm Seiken and I've been in Canada for less than a year, but my English hasn't improved at all and I still reply to "how are you?" with "I'm fine with you."
The top command is loved by all infrastructure engineers, and is probably the first command in Beyond that you learn when operating a server.
In fact, you can do a lot of things with very deep commands, so up until now I had been telling my juniors about my boss's dignity, saying, ``I can do this,'' and my experience as a senior, but I too. I would like to introduce you to a super convenient way to use it that I only recently learned about.
It's a little long, but please bear with me.
When you run the top command normally, the following screen will be displayed.
This alone is packed with important information for server operation, but I think there are many other explanations of how to use the top command, so I will omit it here.
This time, I will make the final display of this top command look like this.
There are 5 points
- Display detailed information such as process arguments
- Add display items and change display order
- Show processes with RUNNING status and sorting columns in bold
- Change the sort order
- Save settings (this is the most important)
Let's try it right away
Display detailed information such as process arguments
This is easy, press the c key
If you run the top command for the first time with the -c option, the same result will occur, but
it is also useful if
Add display items and change display order
In our daily operational work, we not only deal with critical situations such as customer services or processes stopping, but also monitor CPU and memory usage rates to quickly detect situations where server resources are about to run out and take precautions. We are taking measures to prevent failures.
The three indicators often used in this case are CPU usage, memory usage, and SWAP usage, but SWAP usage is not displayed by default in the top command.
However, there are a number of fields on top that are normally hidden, and you can also display SWAP usage.
let's do it
Press Shift+f to change to the Field Management screen as shown below.
This time, I want to add the usage amount of SWAP to the display, so move to the SWAP item using the up and down cursor keys.
When you press the space key, * will be added to the left side of the item, and it will be added to the display items in this state.
If this continues, the display order will be after COMMAND, so it should be displayed after %MEM to make it easier to compare with other indicators.
If you press the right cursor key, the selected state of the cursor will expand a little and you will be able to move it.
Use the up and down cursor keys to move to the desired location
When the movement is complete, press the left cursor key to restore the selection state and fix the position.
Exit the Field Management screen with q or Esc key
The SWAP item has been added next to %MEM.
Display processes with status RUNNING and sorting columns in bold
Now that it's easier to understand visually, let's display the processes with a RUNNING status and the currently sorted column in bold.Let
's also change the color of the displayed screen.
Press Shift+z and the screen will change to the one below.
You can change the color for each field, but the default is red for some reason.
It's a bit toxic, so press 7 to change it to white.
The "Summary Data" field above is also red, so press "shift+S" to switch the field whose color you want to change, and press 7 in the same way to change it to white.
If you press shift+s, "1) Select a target asu an upper case letter...." will change, it's a little hard to understand.
If you enter a number, it will change to the color you like. I tried a lot of things, but in the end I settled on 7 (white)
. There are many other colors besides white, so you can choose whatever color you like here.
To save this state and exit, press "Enter"
It is a success if the color settings of the screen you selected earlier are saved.
Furthermore, from here, let's display the processes whose status is RUNNING in bold, and also display the field that is currently being sorted in bold.
Pressing the x key will make the field you are sorting bold, and pressing the y key will show processes with a status of RUNNING in bold.
It's a little hard to tell on a Mac terminal, but...the sorted field (%CPU) and the process (in this case, the top command) whose status (S) is RUNNING(R) are highlighted in bold. Do you see that
Change the sort order
I think the usual top command has become much easier to use so far.
In our daily operations, we need to investigate the cause of alerts that occur on customer servers, so we often want to sort by memory usage or SWAP usage instead of just displaying the default CPU usage. .
In such a case, you can move the field to be sorted to the left or right using "shift+<" or "shift+>"
It's a little hard to understand, but I tried changing the sort order as %CPU → %MEM → SWAP with shift+>
There are also shortcuts: shift+p (sort by %CPU) and shift+m (sort by %MEM).
Save settings (this is the most important)
Finally, let's save the settings we have made so far.We
have tried changing the display in various ways, but if you exit the top command as it is, the next time you hit the top command it will return to its original state.
It is a hassle to repeat the steps up to this point every time, so
If you want to save the current display state, press "shift+w"
A file called .toprc is generated in the home directory of the user running the top command to save the current settings.
By the way, some of the explanations so far are specific to the top command of RedHat7 series (CentOS7, Amazon Linux2).The
top command of another OS may require slightly different operations, but it can do almost the same thing, so
how about other OS's? Are you going to do it? If you have such a request (although it probably won't), I will respond.
This will help you prepare for monitoring alerts starting tomorrow!
Maybe it's just us, but we hope this article helps someone.