how to make curl request undisturbed in a bash script?

0

In a bash script, I want to make a curl request that the user should not disturb.

trap "cleanup" 1 2 3 13 15

TMP_OUT=$(curl -H "Content-Type: application/json" -X POST -d "$DATA" "${HOST}:${PORT}"'/main/store' 2>/dev/null &)
 wait $!
 if [ $? -ne 0 ]
 then
        fatal "Something went wrong connecting to the service."
 fi

How do I make this work when the service doesn't work? The wait returns 0 even when the rc from the curl request is 7 and the script continues, which shouldn't happen.

Sk Sk

Posted 2018-10-12T16:47:17.143

Reputation: 1

Attempting to background commands inside $( ) doesn't really work, because it needs to wait for them to finish to capture their output. What are you actually trying to accomplish, and what do you mean by "that the user should not disturb"? – Gordon Davisson – 2018-10-12T17:47:39.287

Answers

0

Try something like this:

TMP="$(mktemp)"

curl ifconfig.co 2>/dev/null >"${TMP}" &

wait $!
echo $?

read MY_IP < "${TMP}"
rm "${TMP}"
unset TMP

echo ${MY_IP}

Fundamentally, you need to keep the interesting process as a direct child of "this" shell.

Here we redirect its output to a temporary file, and ingest it later using read.

Attie

Posted 2018-10-12T16:47:17.143

Reputation: 14 841

thanks for the prompt reply, it seems to work but I've got: wait: pid 1541 is not a child of this shell – Sk Sk – 2018-10-12T19:16:14.047