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! |
Welcome to LQ..
Quote:
top -b | head -15 | tail -9 >> cpu_load_monitor_output.txt Hope it helps |
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! |
Quote:
|
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. |