根据 pereal 的回答,我已经修补了一个可以使用的脚本。让我们称之为wait-for-disk-idle
。这种方法的缺点是它自己需要初始化时间。在有效采样“采样时间”的同时执行两次“采样时间”。这是 iostat 的局限性。
(是的,一定是bash,不是sh)
#! /bin/bash
USAGE="Usage: `basename $0` [-t sample time] [-p disk IO percent threshold] disk-device"
time=3
percent=10
# Parse command line options.
while getopts ":t:" OPT; do
case "$OPT" in
t)
time=$OPTARG
;;
:)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
\?)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
esac
done
while getopts ":p:" OPT; do
case "$OPT" in
p)
percent=$OPTARG
;;
:)
;;
\?)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
esac
done
# Remove the switches we parsed above.
shift `expr $OPTIND - 1`
# We want at least one non-option argument.
# Remove this block if you don't need it.
if [ $# -eq 0 ]; then
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
fi
# echo percent: $percent, time: $time, disk: $1
while [[ $(iostat -d -x $time 2 $1 |
sed -n 's/.*[^0-9]\([0-9][0-9]*\),[^,]*$/\1/p' | tail -1) > $percent
]]; do
# echo wait
done