查询Linux系统下CPU及内存信息脚本

  • Post author:
  • Post category:linux




创建脚本存放路径

mkdir -p /data/scripts



编辑shell脚本

vim /data/scripts/cpu_mem_info.sh
#! /bin/bash
# Display the CPU and Memory information of the current machine.
# Edited on Dec 25, 2018
phy_cpu_total=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
phy_cpu_per_core=`cat /proc/cpuinfo | grep "cpu cores" | uniq | awk -F ":" '{print $2}'`
logi_cpu_total=`cat /proc/cpuinfo | grep "processor" | wc -l`
cpu_name=`cat /proc/cpuinfo | grep "model name" | uniq | awk -F ":" '{print $2}'`
memory_k=`cat /proc/meminfo | head -1 | awk -F ":" '{print $2}' | awk '{print $1}'`
memory_g=`awk 'BEGIN{printf "%.2f\n",'$memory_k'/'1048576'}'`
swap_k=`cat /proc/meminfo | grep "SwapTotal:" | awk -F ":" '{print $2}' | awk '{print $1}'`
swap_g=`awk 'BEGIN{printf "%.2f\n",'$swap_k'/'1048576'}'`

echo -e "\n物理CPU总数为 ${phy_cpu_total} 个。"
echo -e "每颗物理CPU的核心数为 ${phy_cpu_per_core} 个。"
echo -e "逻辑CPU的总数为 ${logi_cpu_total} 个。"
echo -e "CPU型号为 ${cpu_name} 。"
echo -e "物理内存总数大约为 ${memory_g} GB。"
echo -e "虚拟内存总数大约为 ${swap_g} GB。\n"



执行脚本

bash /data/scripts/cpu_mem_info.sh

效果如图:



版权声明:本文为monk2014原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。