LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 09-15-2009, 11:38 PM   #1
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: xandros
Posts: 4
Thanked: 0
programming a cpu load generator with bash script


[Log in to get rid of this advertisement]
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!
linux petee is offline  
Tag This Post , , , ,
Reply With Quote
Old 09-16-2009, 12:56 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Mumbai, India
Distribution: Fedora 9, RHEL-5.3, SUSE
Posts: 213
Thanked: 8
Welcome to LQ..


Quote:
Originally Posted by petee View Post


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
linux vinaytp is offline     Reply With Quote
Thanked by:
Old 09-16-2009, 04:23 AM   #3
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: xandros
Posts: 4
Thanked: 0

Original Poster
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!
windows_xp_2003 petee is offline     Reply With Quote
Old 09-16-2009, 05:10 AM   #4
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,846
Blog Entries: 6
Thanked: 226
Quote:
Originally Posted by petee View Post
unless anyone knows of an easier way?
vmstat or sar?
linux catkin is offline     Reply With Quote
Thanked by:
Old 09-16-2009, 09:24 PM   #5
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: xandros
Posts: 4
Thanked: 0

Original Poster
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!
linux petee is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - progressive world list generator musther Programming 2 01-10-2008 05:22 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 08:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration