Linux占用CPU脚本
完整的CPU占用控制脚本#!/bin/bash # cpu_controller.sh - CPU占用控制脚本 SCRIPT_NAMEcpu_stress_worker PID_FILE/tmp/cpu_stress.pid LOG_FILE/tmp/cpu_stress.log # 默认配置 DEFAULT_CORES$(nproc) DEFAULT_USAGE50 # CPU占用工作函数 cpu_worker() { local usage$1 local busy_time$((usage * 10000)) local idle_time$(((100 - usage) * 10000)) while true; do # 忙循环 end$(( $(date %s%N) busy_time * 1000 )) while [ $(date %s%N) -lt $end ]; do : done # 空闲 sleep $(echo scale6; $idle_time / 1000000 | bc 2/dev/null || echo 0.5) done } # 启动CPU占用 start_stress() { local cores${1:-$DEFAULT_CORES} local usage${2:-$DEFAULT_USAGE} # 检查是否已经在运行 if [ -f $PID_FILE ] kill -0 $(cat $PID_FILE) 2/dev/null; then echo CPU占用已在运行中请先停止 return 1 fi echo 启动CPU占用: ${cores}核心, ${usage}%占用率 # 启动多个工作进程 $PID_FILE for ((i0; icores; i)); do cpu_worker $usage echo $! $PID_FILE done echo 已启动PID文件: $PID_FILE echo 使用 $0 stop 停止 echo 使用 $0 status 查看状态 } # 停止CPU占用 stop_stress() { if [ ! -f $PID_FILE ]; then echo 没有找到运行中的CPU占用进程 return 1 fi echo 停止CPU占用... while read pid; do kill $pid 2/dev/null echo 已停止进程 $pid done $PID_FILE rm -f $PID_FILE echo 所有进程已停止 } # 查看状态 status_stress() { if [ -f $PID_FILE ] [ -s $PID_FILE ]; then local running0 while read pid; do if kill -0 $pid 2/dev/null; then ((running)) fi done $PID_FILE if [ $running -gt 0 ]; then echo CPU占用运行中: $running 个进程 echo 当前CPU使用率: top -bn1 | grep Cpu(s) | head -1 else echo CPU占用未运行 rm -f $PID_FILE fi else echo CPU占用未运行 fi } # 调整CPU占用率 adjust_usage() { local new_usage$1 if [ ! -f $PID_FILE ] || [ ! -s $PID_FILE ]; then echo 没有运行中的CPU占用进程 return 1 fi # 获取当前核心数 local cores0 while read pid; do if kill -0 $pid 2/dev/null; then ((cores)) fi done $PID_FILE echo 调整CPU占用率到 ${new_usage}%核心数: $cores # 停止旧进程 stop_stress # 启动新进程 start_stress $cores $new_usage } # 主函数 case ${1:-} in start) start_stress ${2:-$DEFAULT_CORES} ${3:-$DEFAULT_USAGE} ;; stop) stop_stress ;; restart) stop_stress sleep 1 start_stress ${2:-$DEFAULT_CORES} ${3:-$DEFAULT_USAGE} ;; status) status_stress ;; adjust) if [ -z $2 ]; then echo 用法: $0 adjust 占用率1-100 exit 1 fi adjust_usage $2 ;; *) echo 用法: $0 {start|stop|restart|status|adjust} echo echo 命令: echo start [核心数] [占用率] - 启动CPU占用 echo stop - 停止CPU占用 echo restart [核心数] [占用率] - 重启CPU占用 echo status - 查看状态 echo adjust 占用率 - 调整CPU占用率 echo echo 示例: echo $0 start - 使用默认配置启动 echo $0 start 2 80 - 2个核心80%占用率 echo $0 adjust 30 - 调整占用率为30% echo $0 stop - 停止 exit 1 ;; esac使用方法保存并设置权限# 保存脚本 vim cpu_controller.sh # 添加执行权限 chmod x cpu_controller.sh基本操作# 启动默认所有核心50%占用率 ./cpu_controller.sh start # 启动指定核心数和占用率 ./cpu_controller.sh start 2 80 # 2个核心80%占用率 ./cpu_controller.sh start 4 30 # 4个核心30%占用率 # 查看状态 ./cpu_controller.sh status # 动态调整占用率无需重启 ./cpu_controller.sh adjust 90 # 调整为90% ./cpu_controller.sh adjust 10 # 调整为10% # 停止 ./cpu_controller.sh stop # 重启 ./cpu_controller.sh restart 2 60后台运行示例# 后台启动 nohup ./cpu_controller.sh start 2 70 # 查看日志 tail -f /tmp/cpu_stress.log # 停止后台进程 ./cpu_controller.sh stop备注启动的时候报错bash: ./cpu_controller.sh/bin/bash^M解释器错误: 没有那个文件或目录 # 1. 先转换文件格式 sed -i s/\r$// cpu_controller.sh # 2. 添加执行权限 chmod x cpu_controller.sh # 3. 验证文件格式应该看不到^M cat -A cpu_controller.sh | head -1 # 4. 运行脚本 ./cpu_controller.sh start