I'm trying to check the number of running and queued PBS jobs by parsing the output of qstat -tn1
from a bash
script.
So far, this has worked:
count ()
{
qstat -tn1 | awk '
BEGIN { R = 0; Q = 0; }
$10 == "R" { R++ }
$10 == "Q" { Q++ }
END { print R, Q }'
}
if read -r R Q < <(count)
...
However, I see that qstat
occasionally fails for unknown reasons. In that case, it prints nothing to stdout
and some error message to stderr
, and exits with a non-zero status (fairly standard). However, awk
doesn't know that qstat
failed, and happily prints 0 0
for the empty input it received. Then read
assigns 0 to both R
and Q
without knowing that qstat
actually failed.
- I need to initialize
R
andQ
with 0 in theBEGIN
block of theawk
script, because there may be no running processes or no queued processes, and I need to print0
, not just an empty string, for the number of such processes. - I could do
set -o pipefail
, which would allowcount
to exit with a non-zero status, butread
cannot see the exit status, andawk
gets executed and prints0 0
for the empty input anyway. - I could try a named pipe and subprocesses, but having to manage them feels like an overkill too complicated.
Is there any good way to allow the caller of count
to detect its failure?