Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check the occupancy of CPU, memory and swap partitions under Linux

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to check the occupancy rate of CPU, memory and swap partition under Linux". In daily operation, I believe many people have doubts about how to view the occupancy rate of CPU, memory and swap partition under Linux. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to check the occupancy rate of CPU, memory and swap partition under Linux". Next, please follow the editor to study!

Method-1: how to check the memory usage under Linux?

We can use a combination of the following commands to do this. In this method, we use a combination of free and awk commands to obtain memory usage.

If you are looking for other articles about memory, you can navigate to the link below. These articles have free commands, smem commands, ps_mem commands, vmstat commands, and a variety of ways to view the physical memory size.

To get the memory usage that does not contain a percentage symbol:

$free-t | awk'NR = = 2 {print "Current Memory Utilization is:" $3 free "100}'or $free-t | awk 'FNR = = 2 {print" Current Memory Utilization is: "$3 print 2" 100}' Current Memory Utilization is: 20.4194

To get the occupancy rate of a swap partition that does not contain a percentage symbol:

$free-t | awk'NR = = 3 {print "Current Swap Utilization is:" $3 free "100}'or $free-t | awk 'FNR = = 3 {print" Current Swap Utilization is: "$3 print 2" 100}' Current Swap Utilization is: 0

To get the memory usage that contains the percentage symbol and retains two decimal places:

$free-t | awk'NR = = 2 {printf ("Current Memory Utilization is:% .2f%"), $3Current Memory Utilization is / 2f%}'or $free-t | awk 'FNR = = 2 {printf ("Current Memory Utilization is:% .2f%"), $3pm / 2f}' Current Memory Utilization is: 20.42%

To get the swap partition occupancy rate that contains the percentage symbol and retains two decimal places:

$free-t | awk'NR = = 3 {printf ("Current Swap Utilization is:% .2f%"), $3max / 2f%}'or $free-t | awk 'FNR = = 3 {printf ("Current Swap Utilization is:% .2f%"), $3pm / 2f%}' Current Swap Utilization is: 0.00%

If you are looking for other articles about swapping partitions, you can navigate to the link below. These links have multiple ways to create and extend swap partitions using LVM (logical Volume Management), to create or extend swap partitions, and to create / delete and mount swap partition files.

Typing the free command will better explain:

$free total used free shared buff/cache availableMem: 15867 3730 9868 1189 2269 10640Swap: 17454 0 17454Total: 33322 3730 27322

Details are as follows:

Free: is a standard command for viewing memory usage under Linux.

Awk: a powerful command specifically for text data processing.

FNR = = 2: this command gives the number of lines for each input file. It is basically used to pick out a given row (for this case, it selects the line with line number 2)

NR = = 2: this command gives the total number of rows processed. It is basically used to filter the given row (for this case, it selects the line with line number 2)

$3Universe 100: this command divides column 3 by column 2 and multiplies the result by 100.

Printf: this command is used to format and print data.

% .2f%: by default, it retains a floating-point number of 6 places after printing the decimal point. Use the following format to constrain decimal places.

Method-2: how to check the memory usage under Linux?

We can use a combination of the following commands to do this. In this approach, we use a combination of free, grep, and awk commands to obtain memory usage.

To get the memory usage that does not contain a percentage symbol:

$free-t | grep Mem | awk'{print "Current Memory Utilization is:" $3 Current Memory Utilization is: 20.4228

To get the occupancy rate of a swap partition that does not contain a percentage symbol:

$free-t | grep Swap | awk'{print "Current Swap Utilization is:" $3 Current Swap Utilization is: 0

To get the memory usage that contains the percentage symbol and retains two decimal places:

$free-t | grep Mem | awk'{printf ("Current Memory Utilization is:% .2f%"), Current Memory Utilization is: 20.43%

To get the swap space occupancy that contains the percentage sign and retains two decimal places:

$free-t | grep Swap | awk'{printf ("Current Swap Utilization is:% .2f%"), $3 Current Swap Utilization is: 0.005% method-1: how to check the occupancy rate of CPU under Linux?

We can do this using a combination of the following commands. In this approach, we use a combination of top, print, and awk commands to obtain CPU occupancy.

If you are looking for other articles about CPU, you can navigate to the link below. These articles include top command, htop command, atop command, and Glances command.

If you are showing multiple CPU in the output, you need to use the following method.

$top-b-N1 | grep ^% Cpu%Cpu0: 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1: 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2: 0.0 us, 0.0 sy 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 5.3 si, 0.0 st%Cpu3: 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu4: 10.5 us, 15.8 sy, 0.0 ni, 73.7 id, 0.0 wa, 0.0 hi, 0.0 si 0.0 st%Cpu5: 0.0 us, 5.0 sy, 0.0 ni, 95.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu6: 5.3 us, 0.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu7: 5.3 us, 0.0 sy, 0.0 ni, 94.7 id 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

To get the CPU occupancy that does not contain a percentage symbol:

$top-b-N1 | grep ^% Cpu | awk'{cpu+=$9} END {print "Current CPU Utilization is:" 100-cpu/NR} 'Current CPU Utilization is: 21.05

To get the CPU occupancy that contains the percentage sign and retains two decimal places:

$top-b-N1 | grep ^% Cpu | awk'{cpu+=$9} END {printf ("Current CPU Utilization is:% .2f%"), 100-cpu/NR} 'Current CPU Utilization is: 14.81% method-2: how to check the occupancy rate of CPU under Linux?

We can do this using a combination of the following commands. In this approach, we use a combination of top, print/printf, and awk commands to obtain CPU occupancy.

If all the CPU cases are shown together in a single output, then you need to use the following method.

$top-b-N1 | grep ^% Cpu%Cpu (s): 15.3 us, 7.2 sy, 0.8 ni, 69.0 id, 6.7 wa, 0.0 hi, 1.0 si, 0.0 st

To get the CPU occupancy that does not contain a percentage symbol:

$top-b-N1 | grep ^% Cpu | awk'{print "Current CPU Utilization is:" 100MB 8} 'Current CPU Utilization is: 5.6

To get the CPU occupancy that contains the percentage sign and retains two decimal places:

$top-b-N1 | grep ^% Cpu | awk'{printf ("Current CPU Utilization is:% .2f%"), 100Muth8} 'Current CPU Utilization is: 5.40%

Here are some details:

Top: a very good command for viewing processes running on the current Linux system.

-b: option allows the top command to switch to batch mode. It is very useful when you run the top command from the local system to the remote system.

-N1: iterations.

^% Cpu: filter lines that start with% CPU.

Awk: a powerful command specifically used for text data processing.

Cpu+=$9: for each row, add column 9 to the variable cpu.

Printf: this command is used to format and print data.

% .2f%: by default, it retains a floating point of 6 places after printing the decimal point. Use the following format to limit the number of decimal places.

100-cpu/NR: finally prints out the average occupancy of CPU, which is subtracted by 100 and divided by the number of lines.

At this point, the study on "how to check the occupancy of CPU, memory and swap partitions under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report