Solved

Need help writing a simple bash script

Posted on 2010-09-22
13
826 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

831 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