LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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

Reply
 
Search this Thread
Old 09-15-2009, 10:38 PM   #1
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: ubuntu
Posts: 9

Rep: Reputation: 1
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!
 
Old 09-15-2009, 11:56 PM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
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
 
Old 09-16-2009, 03:23 AM   #3
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: ubuntu
Posts: 9

Original Poster
Rep: Reputation: 1
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!
 
Old 09-16-2009, 04:10 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by petee View Post
unless anyone knows of an easier way?
vmstat or sar?
 
Old 09-16-2009, 08:24 PM   #5
petee
LQ Newbie
 
Registered: Sep 2009
Location: adelaide, aus
Distribution: ubuntu
Posts: 9

Original Poster
Rep: Reputation: 1
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!
 
  


Reply

Tags
bash, cpu, load, monitor


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 04:22 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


All times are GMT -5. The time now is 11:18 PM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration