ref: 2ebee720a8af84e6f1a4d4e390f723526553b5e0
dir: /bin/rc/txcheck/
#!/bin/rc flagfmt='r procroot, u unit, m maxsize, D delay' args='txpid txfd' # variables and functions txpid=() txfd=() pidfile=() delay=() fdpath=() maxsz=() unitfrac=1 fn usage { aux/usage exit usage } fn fatal { echo $* >[1=2] exit $"* } fn isnum { echo $1 | grep -s '^[0-9]+$' } fn isfloat { echo $1 | grep -s '^[0-9]+\.[0-9]+$' } # argument parsing if(! eval `{aux/getflags -r /proc -u b -D 3 $*}) usage txpid=$1 txfd=$2 if(~ $#txpid 0 || ~ $#txfd 0) usage # util and sanity checks fdpath=$flagr^'/'^$txpid^'/fd' if(! test -r $fdpath) fatal 'unable to read' $fdpath switch($flagu) { case b or B unitfrac='1' case kb or KB unitfrac='1024' case mb or MB unitfrac='1024 * 1024' case * if(! isnum $flagu) fatal 'unknown unit or not a number' unitfrac=$flagu } if(! isnum $flagD) fatal 'delay is not a number' delay=$flagD if(! isfloat $flagm) fatal 'maximum size is not a float' maxsz=$flagm pidfile=/tmp/txcheck.$txpid touch $pidfile while(test -f $pidfile) { fdamt=`{cat $fdpath | grep '^ '^$txfd | awk '{print $9}'} amt=`{echo 'scale = 2;' $fdamt '/ (' $unitfrac ')' | bc} echo ($amt $maxsz) sleep $delay }