创建脚本存放路径
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
效果如图:
本文介绍了一个用于获取服务器CPU和内存详细信息的Shell脚本。该脚本可以显示物理CPU数量、每颗CPU的核心数、逻辑CPU总数、CPU型号、物理内存和虚拟内存大小等关键信息。

407

被折叠的 条评论
为什么被折叠?



