How to create a CPU spike with a bash command

后端 未结 23 1125
小蘑菇
小蘑菇 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:45

    To enhance dimba's answer and provide something more pluggable (because i needed something similar). I have written the following using the dd load-up concept :D

    It will check current cores, and create that many dd threads. Start and End core load with Enter

    #!/bin/bash
    
    load_dd() {
        dd if=/dev/zero of=/dev/null
    }
    
    fulload() {
        unset LOAD_ME_UP_SCOTTY
        export cores="$(grep proc /proc/cpuinfo -c)"
        for i in $( seq 1 $( expr $cores - 1 ) )
          do
        export LOAD_ME_UP_SCOTTY="${LOAD_ME_UP_SCOTTY}$(echo 'load_dd | ')"
      done
            export LOAD_ME_UP_SCOTTY="${LOAD_ME_UP_SCOTTY}$(echo 'load_dd &')"
        eval ${LOAD_ME_UP_SCOTTY}
    }
    
    echo press return to begin and stop fullload of cores
      read
      fulload
      read
      killall -9 dd
    
    0 讨论(0)
  • 2020-11-30 16:46

    You can also do

    dd if=/dev/zero of=/dev/null
    

    To run more of those to put load on more cores, try to fork it:

    fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd
    

    Repeat the command in the curly brackets as many times as the number of threads you want to produce (here 4 threads). Simple enter hit will stop it (just make sure no other dd is running on this user or you kill it too).

    0 讨论(0)
  • 2020-11-30 16:46

    Although I'm late to the party, this post is among the top results in the google search "generate load in linux".

    The result marked as solution could be used to generate a system load, i'm preferring to use sha1sum /dev/zero to impose a load on a cpu-core.

    The idea is to calculate a hash sum from an infinite datastream (eg. /dev/zero, /dev/urandom, ...) this process will try to max out a cpu-core until the process is aborted. To generate a load for more cores, multiple commands can be piped together.

    eg. generate a 2 core load: sha1sum /dev/zero | sha1sum /dev/zero

    0 讨论(0)
  • 2020-11-30 16:47

    I would split the thing in 2 scripts :

    infinite_loop.bash :

    #!/bin/bash
    while [ 1 ] ; do
        # Force some computation even if it is useless to actually work the CPU
        echo $((13**99)) 1>/dev/null 2>&1
    done
    

    cpu_spike.bash :

    #!/bin/bash
    # Either use environment variables for NUM_CPU and DURATION, or define them here
    for i in `seq ${NUM_CPU}` : do
        # Put an infinite loop on each CPU
        infinite_loop.bash &
    done
    
    # Wait DURATION seconds then stop the loops and quit
    sleep ${DURATION}
    killall infinite_loop.bash
    
    0 讨论(0)
  • 2020-11-30 16:48

    to increase load or consume CPU 100%

    sha1sum /dev/zero &
    

    then you can see CPU uses by typing command

    top
    

    to release the load

    killall sha1sum
    
    0 讨论(0)
  • 2020-11-30 16:50

    Just paste this bad boy into the SSH or console of any server running linux. You can kill the processes manually, but I just shutdown the server when I'm done, quicker.

    Edit: I have updated this script to now have a timer feature so that there is no need to kill the processes.

    read -p "Please enter the number of minutes for test >" MINTEST && [[ $MINTEST == ?(-)+([0-9]) ]]; NCPU="$(grep -c ^processor /proc/cpuinfo)";  ((endtime=$(date +%s) + ($MINTEST*60))); NCPU=$((NCPU-1)); for ((i=1; i<=$NCPU; i++)); do while (($(date +%s) < $endtime)); do : ; done & done
    
    0 讨论(0)
提交回复
热议问题