From 5bc8f0105b51ea0caf902ee4741ea274956400e8 Mon Sep 17 00:00:00 2001 From: Andrea Bolognani Date: Mon, 16 Dec 2019 23:43:23 +0100 Subject: [PATCH] baraction: Simplify update loop The current loop is a bit awkward for a number of reasons: * iostat exits after a number of iterations, so to keep the script going we need to run it in a nested loop; * we also run iostat concurrently using the |& syntax, which in addition to being non-portable requires us to set up a cleanup path so that we don't leave stray processes around when spectrwm is restarted; * due to the fact that iostat prints its headers again every 20 records, we have to keep careful track of the number of iterations - something which has proven to be error-prone. Once we do away with the idea of iostat driving the update loop, all of these issues disappear and the code becomes much simpler. We still cache battery data for 11 seconds, which is the same amount as before; tweaking it further, if desired, will be trivial now. This patch is better viewed with 'git show -w'. --- baraction.sh | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/baraction.sh b/baraction.sh index 5fc2a56..9c89388 100644 --- a/baraction.sh +++ b/baraction.sh @@ -80,26 +80,20 @@ print_bat() { fi } +# cache the output of apm(8), no need to call that every second. +APM_DATA="" +I=0 while :; do - # instead of sleeping, use iostat as the update timer. - # cache the output of apm(8), no need to call that every second. - /usr/sbin/iostat -C -c 3600 |& # wish infinity was an option - PID="$!" - APM_DATA="" - I=0 - trap "kill $PID; exit" TERM - while read IOSTAT_DATA; do - if [ $(( ${I} % 11 )) -eq 0 ]; then - APM_DATA=`/usr/sbin/apm -alb` - fi - if [ $I -ge 2 ]; then - # print_date - print_mem - print_cpu $IOSTAT_DATA - print_cpuspeed - print_bat $APM_DATA - echo "" - fi - I=$(( ( ${I} + 1 ) % 22 )); - done + IOSTAT_DATA=`/usr/sbin/iostat -C | grep '[0-9]$'` + if [ $I -eq 0 ]; then + APM_DATA=`/usr/sbin/apm -alb` + fi + # print_date + print_mem + print_cpu $IOSTAT_DATA + print_cpuspeed + print_bat $APM_DATA + echo "" + I=$(( ( ${I} + 1 ) % 11 )) + sleep 1 done