Search code examples
bashawkiostat

Redirecting iostat output to a file


Why the output of iostat is not redirected to the file in the following command:

iostat -x 3 | awk '/sda/ { print $11, $12}' > /tmp/disk_utilization 

Is iostat behaving differently than any other command?

Thx in advance


Solution

  • Looks like you need to add a count to tell the stream when to terminate( man iostat ). You're only adding the interval:

    If the interval parameter is specified without the count parameter, the iostat command generates reports continuously. 
    

    When I try:

    iostat -x 3 6 | awk '/sda/ { print $11, $12}' > outfile
    

    the 6 tells iostat to stop after 6 iterations of 3 second intervals. After the iterations iostat completes and I get an output file.

    Alternatively, you could pick some expression to cause the awk script to exit. When I try:

    iostat -x 3 | awk -v max=10 '/sda/ { print $11, $12; max++ } NR > max { exit } ' > outfile2
    

    I get just 3 lines in output2 which makes sense because iostat -x produces about a screens worth of lines of output to the screen.

    You could also force the awk to fflush(stdout) in each print cycle:

    iostat -x 3 | awk '/sda/ { print $11, $12; fflush(stdout) }' > output3
    

    and then close iostat with a <ctrl+c> from the shell.