linux下关闭cpu核心
查看在线CPU核心
cat /sys/devices/system/cpu/online
输入top,启动后按数字1列出所有CPU核
强制CPU5下线(考虑到服务存在多路,应该理解为Core5)
echo 0 > /sys/devices/system/cpu/cpu5/online
一个简单粗暴的脚本 关闭核心数量
#!/bin/bash
# 检查是否有参数传入
if [ "$#" -eq 0 ]; then
# 如果没有参数,显示在线的核心数
ranges=$(cat /sys/devices/system/cpu/online)
# 假设范围是连续的,如"0-3",则计算结束数字+1
last_cpu=$(echo "$ranges" | sed 's/^.*-\([0-9]*\)$/\1/')
echo "Online CPUs: $((last_cpu + 1))"
else
case "$1" in
1)
# 离线 CPU3
echo 0 > /sys/devices/system/cpu/cpu3/online
;;
2)
# 离线 CPU3 和 CPU2
echo 0 > /sys/devices/system/cpu/cpu3/online
echo 0 > /sys/devices/system/cpu/cpu2/online
;;
3)
# 离线 CPU3, CPU2, 和 CPU1
echo 0 > /sys/devices/system/cpu/cpu3/online
echo 0 > /sys/devices/system/cpu/cpu2/online
echo 0 > /sys/devices/system/cpu/cpu1/online
;;
r)
# 在线 CPU1, CPU2, 和 CPU3
echo 1 > /sys/devices/system/cpu/cpu1/online
echo 1 > /sys/devices/system/cpu/cpu2/online
echo 1 > /sys/devices/system/cpu/cpu3/online
;;
*)
# 如果参数不是1, 2, 3, 或 r,则打印帮助信息
echo "Usage: $0 [1|2|3|r]"
echo " 1: Offline CPU3"
echo " 2: Offline CPU3 and CPU2"
echo " 3: Offline CPU3, CPU2, and CPU1"
echo " r: Online CPU1, CPU2, and CPU3"
;;
esac
fi