0

I have a case where a first few command execution attempts after daemon restart are returning 2 (followed by stderr messages), than everything works correctly.

In order to bypass this bug I've come to an idea that I should utilize while/do/done/until functionality with some sleep, but the fun part is that I should have all the data I'd get by the regular execution way:

...
RESULT=$(command param1 param2 2>&1)
SIG=$?
...

So is there a way to execute command param1 param2 repeatedly until exit status is NOT 2 (any given value) and to have STDOUT+STDERR in a variable RESULT and exit status in variable SIG, as presented?

2 Answers 2

2
outfile=$(mktemp)

while ! command param1 param2 >"$outfile" 2>&1; do
    [ "$?" -ne 2 ] && break
    sleep 10
done

result=$(<"$outfile")
rm "$outfile"

This would run the command over and over again (with a 10 second delay in-between each time), until it succeeds or at least does not return a 2 as exit status.

The output of the command is written to a temporary file which, after the loop, is read into a variable and deleted. The reading into a variable bit requires bash, the way I've written it here.

1
until [ "$SIG" != "2" ]; do
   command param1 param2
   SIG=$?
done

This will run the command every time until it returns a number different from 2.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .