2

I'm using iostat to get the current disk load each second with iostat -dx 1 (specially, %util column). However, I'd like to put this in a bash script and control the interval with the watch command such as: watch -n 1 ./script.sh.

Running the following in script.sh won't print a thing:

io_load=`iostat -dx 1`
echo $io_load

Any ideas?

  • io_load=$(iostat -dx 1) – phe Dec 30 '14 at 15:43
  • no. does not work. What I'd like it to run script.sh with watch – Jay Dec 30 '14 at 16:00
  • Why not simply use watch iostat -dx? – michas Dec 30 '14 at 16:02
  • Try it yourself. And then run iostat -dx 1. You will realize that with your suggestion won't update the output – Jay Dec 30 '14 at 16:03
0

The man page of iostat says:

   The interval parameter specifies the amount of time in seconds between each
   report.  The  first  report  contains  statistics for the time since system
   startup (boot), unless the -y option is used (in this case, this report  is
   omitted).   Each subsequent report contains statistics collected during the
   interval since the previous report. 

This means the first output of iostat -dx 1 will be the same as iostat -dx, but subsequent outputs are different. - You cannot reproduce this behavior using watch.

1

Your iostat -dx 1 will not terminate and continuously report values. (The 1 refers to the interval to to the count.)

You probably want something like

io_load=$(iostat -dx)
echo "$io_load"
  • nope. The output never changes. Try it yourself. Running the code you suggest and iostat -dx 1 – Jay Dec 30 '14 at 15:57
  • Try iostat -dx 1 and iostat -dx. The first will not terminate the second will. – michas Dec 30 '14 at 15:59
  • I did try. But running watch on the script won't provide an updated output – Jay Dec 30 '14 at 16:02
  • It does. Just the output doesn't really change :) You can check it works by using watch -n 1 "date && iostat -dx" – The Godfather Aug 21 '18 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.