How to create a CPU spike with a bash command

后端 未结 23 1182
小蘑菇
小蘑菇 2020-11-30 16:11

I want to create a near 100% load on a Linux machine. It\'s quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of

23条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 16:39

    Utilizing ideas here, created code which exits automatically after a set duration, don't have to kill processes --

    #!/bin/bash
    echo "Usage : ./killproc_ds.sh 6 60  (6 threads for 60 secs)"
    
    # Define variables
    NUM_PROCS=${1:-6} #How much scaling you want to do
    duration=${2:-20}    # seconds
    
    function infinite_loop {
    endtime=$(($(date +%s) + $duration))
    while (($(date +%s) < $endtime)); do
        #echo $(date +%s)
        echo $((13**99)) 1>/dev/null 2>&1
        $(dd if=/dev/urandom count=10000 status=none| bzip2 -9 >> /dev/null) 2>&1 >&/dev/null
    done
    echo "Done Stressing the system - for thread $1"
    }
    
    
    echo Running for duration $duration secs, spawning $NUM_PROCS threads in background
    for i in `seq ${NUM_PROCS}` ;
    do
    # Put an infinite loop
        infinite_loop $i  &
    done
    

提交回复
热议问题