Solved

Need help writing a simple bash script

Posted on 2010-09-22
13
827 Views
Last Modified: 2013-12-26
Hi Guys,

I need to collect iostat information every 15 minutes, and also print the timestamp for each collection interval. I am planning to write a simple shell script that will output iostat to a file and also collect the timestamp for each iostat interval...I will then add this to crontab to run every 15 mins...this is what I have so far:

vi run_perf.sh
iostat -xn 3 10 >> /syb/homedir/monitoring/iostat.txt

I'm not used to writing scripts but I want is for my script to append iostat to a file upon every execution and also print timestamp...

Any help is welcome!

Many Thanks.
0
Comment
Question by:SybaseUk
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 9

Expert Comment

by:rfportilla
ID: 33732926
Does this work?

echo $(date) $(iostat -xn 3 10) >>  /syb/homedir/monitoring/iostat.txt

0
 

Author Comment

by:SybaseUk
ID: 33732966
No, that gives me funny output in my iostat.txt file
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33733065
Try
( date ; iostat -xn 3 10 ) >> /syb/homedir/monitoring/iostat.txt
wmp

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33733088
.. or rfportilla's solution, a bit modified -
echo "$(date)  $(iostat -xn 3 10)" >>  /syb/homedir/monitoring/iostat.txt
 
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33733414
the solution from woolmilkporc will work but there is a slight tweak to the solution.

echo "$(date) $(iostat -xn 1 1)" >> iostat.txt

3 10 will run it 10 times every 3 second..
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33733449
OK,
if 10 iterations every 3 seconds including 10 timestamps are desired, you could do this -
for i in $(seq 1 10) ; do (date ; iostat -xn 1 1) >>/syb/homedir/monitoring/iostat.txt; sleep 3; done
 
 
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33733590
i think author wants to collect one sample every 15 minutes so adding this to cron as below will do

*/15 * * * * echo "$(date) $(iostat -xn 1 1)" >> /root/iostat.txt
0
 

Author Comment

by:SybaseUk
ID: 33733617
What I want is 10 iterations and 3 seconds apart so the iostat command that I want is:

iostat -xn 3 10

this command will run every 15 mins by putting it into crontab, as:

15 * * * * * /syb/homedir/mon/run_perf.sh


the only problem is that I do not know how to add a timestamp for each collection interval (i.e. a timestamp for every 15 minute run)....

any ideas?
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33733657
in that case the solution id 33733449 by woolmilkporc is perfect for you.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 33733691
One single timestamp for every 15 minute run ( i.e. one timestamp per 10 iterations) - that's one of my solutions #33733065 or #33733088
 
0
 

Author Comment

by:SybaseUk
ID: 33733741
can u be a bit clear please? I am not that familiar with EE....where are your solutions?

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 33733792
( date ; iostat -xn 3 10 ) >> /syb/homedir/monitoring/iostat.txt
echo "$(date)  $(iostat -xn 3 10)" >>  /syb/homedir/monitoring/iostat.txt
The numbers I mentioned represent Comment IDs found in the header bar of the respective comment (preceeding "Author:")
 
 
0
 

Author Comment

by:SybaseUk
ID: 33733990
ok thank you...that worked fine.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question