0

I have two scripts to put a spike on CPU .

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

The script was earlier working fine. BUt now the scriptis not working fine. Its giving me an error :

./cpu_spike.bash: line 5: syntax error near unexpected token `infinite_loop.bash'
./cpu_spike.bash: line 5: `    infinite_loop.bash &'

1 Answer 1

0

The error is in the following line:

for i in `seq ${NUM_CPU}` : do

You need to terminate the for using a ;, not :. Say:

for i in `seq ${NUM_CPU}` ; do

: is a null command.


Moreover, saying:

while [ 1 ] ; do command ; done

to simulate an infinite loop is incorrect while it actually does produce one. You might observe that saying:

while [ 0 ] ; do command ; done

would also result in an infinite loop. The correct way would be to say:

while true ; do command; done

or

while : ; do command; done

or

while ((1)); do command; done

For other variations of producing an infinite loop, see this answer.

Sign up to request clarification or add additional context in comments.

7 Comments

I just want to put a load on cpu using some script that goes in an infinite loop
Its giving me an error : seq: missing operand Try seq --help' for more information. ^C`
@user3086014 You would get this error if you haven't set the variable NUM_CPU.
@user3086014 Before any line using the variable NUM_CPU, set it to value. For example, saying NUM_CPU=4 would set the value to 4.
but earlier the script was working fine without setting the value for CPU
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.