Question

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

Was it helpful?

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.

OTHER TIPS

The cause of your problem is that the pipe is buffered (probably 4k), so if you wait long enough for the output to be flushed to the file it will no longer be empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top