LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   programming a cpu load generator with bash script (https://www.linuxquestions.org/questions/linux-newbie-8/programming-a-cpu-load-generator-with-bash-script-755518/)

petee 09-15-2009 10:38 PM

programming a cpu load generator with bash script
 
hi all! i should say first that im very new to linux so please try to be explanatory with the answers you give otherwise im bound to ask a lot of annoying questions ;)
im trying to write a bash script to generate a cpu load (at this stage i dont mind if its only approximate). my backgroud is in control systems engineering so once ive got the linux basics down then i will [hopefully] be able to refine the precision of the program. anyway, so far ive written the following 3 scripts:

#######cpu_load_caller.sh#######
echo "" > status.txt # erase the contents of status.txt
./cpu_load_monitor.sh &
i=0
while [ $i -lt $1 ]
do
./cpu_load_generator.sh &
i=`expr $i + 1`
done
exit 0
################################

#######cpu_load_generator.sh####
i=1
while [ $i -lt 500 ]
do
i=`expr $i + 1`
echo "GENERATING LOAD..." >> status.txt
done
exit 0
################################

#######cpu_load_monitor.sh######
i=0
while [ $i -lt 100 ]
do
top | head -15 | tail -9 >> cpu_load_monitor_output.txt
i=`expr $i + 1`
echo "MONITORING..." >> status.txt
done
exit 0
################################

the program is called from the terminal as
cpu_load_caller.sh 100
or 100 can be replaced with another number. theoretically the 100 value should produce 100 processes and thereby increase the cpu load for a while. at this stage im just trying to get it running without feedback - once i've acheived that then i'll clean up the output to cpu_load_monitor_output.txt so that it reflects the total cpu load in a single decimal value per line and then use that in cpu_load_caller.sh to tune the load.

however at the moment the program only spits out the error

top: failed tty get

which i guess means that the top function does not like running in the background? ive read a few similar posts in this forum which suggest using tokens or even moving the seperate shell scripts into functions all within the same file. if this is the way to go please let me know and i'll give it a bash and write back with my new script.

thanks for your time!

vinaytp 09-15-2009 11:56 PM

Welcome to LQ..


Quote:

Originally Posted by petee (Post 3684468)


top | head -15 | tail -9 >> cpu_load_monitor_output.txt

use top in batch mode and try...It will surely works

top -b | head -15 | tail -9 >> cpu_load_monitor_output.txt

Hope it helps

petee 09-16-2009 03:23 AM

ah thanks vinatp! - that works well. both files (cpu_load_monitor_output.txt and status.txt) are now being written to and the cpu load appears to start high and come down towards the end of the cpu_load_monitor_output.txt file.

the step is to determine the overall cpu load by summing the lines output from "top -b | head -xxx | tail -xxx" - unless anyone knows of an easier way? i'll post my solution once i have it :)

cheers again!

catkin 09-16-2009 04:10 AM

Quote:

Originally Posted by petee (Post 3684685)
unless anyone knows of an easier way?

vmstat or sar?

petee 09-16-2009 08:24 PM

thanks for the suggestions catkin. i had a quick look at vmstat and this just seems to divide the present cpu utilisation up - eg into [vs] - time spent running non kernel code, [sy] - time spent running kernel code, [id] - time spent idle...
however im thinking if i do (100 - id) this should give the time spent by the cpu not idle.
sar also looked useful but i think it needs gcc which i couldnt install on xandros :(

so which out of:
* 100-id with vmstat
* summing of all %CPU's from top
would give the more accurate total cpu load? or should they be identical?
thanks in advance once again!


All times are GMT -5. The time now is 01:43 PM.