Link to home
Start Free TrialLog in
Avatar of SybaseUk
SybaseUk

asked on

Need help writing a simple bash script

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.
Avatar of rfportilla
rfportilla
Flag of United States of America image

Does this work?

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

Avatar of SybaseUk
SybaseUk

ASKER

No, that gives me funny output in my iostat.txt file
Avatar of woolmilkporc
Try
( date ; iostat -xn 3 10 ) >> /syb/homedir/monitoring/iostat.txt
wmp

.. or rfportilla's solution, a bit modified -
echo "$(date)  $(iostat -xn 3 10)" >>  /syb/homedir/monitoring/iostat.txt
 
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..
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
 
 
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
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?
in that case the solution id 33733449 by woolmilkporc is perfect for you.
One single timestamp for every 15 minute run ( i.e. one timestamp per 10 iterations) - that's one of my solutions #33733065 or #33733088
 
can u be a bit clear please? I am not that familiar with EE....where are your solutions?

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok thank you...that worked fine.