I am trying to extract some data from a DB and monitor it's progress, since the tool that pulls the data doesn't appear to have a mechanism for doing so. So, I fork the command chain and then continue to count the lines of the output file until it watch the count of rows expected.
The problem is (I believe) I am forking the command chain properly. And, although indeed data does flow, it isn't forking, because the code that monitors the file isn't executing. What am I missing?
I am tagging this with sqlplus
incase someone from that community knows how to do this natively
#!/usr/bin/env zsh
set -e
set -o pipefail
set -x
set -m
function extract() {
local from="${1:?You must supply a query from date as 'YYYY-MM-DD HH24:MI:SS'}"
local to="${2:?You must supply a query from date as 'YYYY-MM-DD HH24:MI:SS'}"
local u="${3:-${OMC_USERNAME:?no db username supplied}}"
local p="${4:-${OMC_PASSWORD:?No db password supplied}}"
local workdir records
workdir="$(mkWorkdir "${from}" "${to}")"
records=$(sqlplus -s "$u/$p@sredb1_high" @getdata.sql "${from}" "${to}" | tail +2 | tr -d ' ')
# the first 2 lines are skipped because, apparently, when we do variable substitution, it lets us know
# on stdout and not stderr
(sqlplus -s "$u/$p@sredb1_high" @getdata.sql "${from}" "${to}" | tail +2 > "${workdir}/data.psv") &
local process=$!
local count="0"
until [ "${count}" -eq "${records}" ]; do
count=$(wc -l "${workdir}/data.psv")
printf '%.2f%%\r' $((count / records))
sleep 5
done
wait $process
}
Output (some execution you see if due to this code being loaded into another script, but I don't believe it's relevant):
./omc.sh extract '2023-05-01 00:00:00' '2023-05-01 23:59:59'
+ [[ 3 == 0 ]]
++ dirname ./omc.sh
+ source ./commands/extract.sh
++ set -e
++ set -o pipefail
++ set -x
++ set -m
+ extract '2023-05-01 00:00:00' '2023-05-01 23:59:59'
+ local 'from=2023-05-01 00:00:00'
+ local 'to=2023-05-01 23:59:59'
+ local u=uuu
+ local p=pppp
+ local workdir records
++ mkWorkdir '2023-05-01 00:00:00' '2023-05-01 23:59:59'
++ local 'from=2023-05-01 00:00:00'
++ local 'to=2023-05-01 23:59:59'
++ isDate '2023-05-01 00:00:00'
++ grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}'
++ isDate '2023-05-01 23:59:59'
++ grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}'
++ local workdir=2023-05-01-00:00:00_2023-05-01-23:59:59
++ test -d 2023-05-01-00:00:00_2023-05-01-23:59:59
++ echo 2023-05-01-00:00:00_2023-05-01-23:59:59
+ workdir=2023-05-01-00:00:00_2023-05-01-23:59:59
++ sqlplus -s uuu/pppp#@sredb1_high @getdata.sql '2023-05-01 00:00:00' '2023-05-01 23:59:59'
++ tail +2
++ tr -d ' '
^C^C^C^C^C^D
I end up killing the process since it's not doing as I expect