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 &'
user3086014
  • 4,241
  • 5
  • 27
  • 56

1 Answers1

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.

Community
  • 1
  • 1
devnull
  • 118,548
  • 33
  • 236
  • 227