3

I'm trying to obtain the current disk I/O usage (in %) from a single command.

Currently I have

iostat -dx /dev/sda 1 | awk  {'print $16'}

which gives me the utilization entry for I/O from iostat. It also keeps updating and giving new entries, that's something I don't want:

%util
0.06

%util
0.00

%util
0.09

What i'm trying to get is just a single line that gives the current I/O usage in percentage. so its output would simply look like this:

0.06
dessert
  • 39,982
Roderik
  • 33

1 Answers1

3

This seems to work for me (in the below examples, the first one was taken with nothing else going on, and the second was taken reading a big huge file):

doug@s15:~/iso$ iostat -dxy 2 1 /dev/sda | grep sda | awk  {'print $14'}
0.00
doug@s15:~/iso$ iostat -dxy 2 1 /dev/sda | grep sda | awk  {'print $14'}
100.00

The command takes one sample over a 2 second interval, and ignores the starting statistics.

NOTE: On my computer I had to use the 14th column instead of the 16th.

Doug Smythies
  • 15,448
  • 5
  • 44
  • 61
  • OP is on 18.04 could that explain column 14 vs 16? – WinEunuuchs2Unix May 16 '19 at 22:55
  • @WinEunuuchs2Unix : Agreed. I was using a 16.04 computer (server). Just for completeness, just now I checked an 18.04 Ubuntu DeskTop computer (and old Apple Mac) and indeed it had 16 columns. I thought that was the case, but just didn't bother to look into it before. – Doug Smythies May 16 '19 at 23:15
  • I'm on 16.04 as is probably half the community so it is good for all interested iostat users to know 16.04=14 and 18.04=16. BTW I just wrote a script right up your CPU alley: https://askubuntu.com/questions/1141605/how-can-i-control-cpu-usage-on-a-chuwi-hi10-pro/1142671#1142671 – WinEunuuchs2Unix May 16 '19 at 23:20
  • to combine this in one awk call using $NF to print the last column no whatter which number this is: iostat -dxy 2 1 /dev/sda | awk '/sda/{print $NF}' – dessert May 17 '19 at 07:30