bashcurldaemon

How to dynamically add daemon & to end of curl


On my first run I need curl to run and wait for response. DB has a trigger that updates a field and I need to wait for that. Then on all my other curls to run on separate threads. I cannot use the parallel options for curl as the version we have does not have it and I cannot update it anyway so I have to go old school and do

for ...
  curl "some curl options" &
end for loop

wait # this waits for all the threads to finish

So I need the "&" sign to be set after the first run. FYI my curl command is 20 lines long and I did try this at the end of the curl command but it doesn't work. Trying single and double quotes around the & sign doesn't work either and give me "unexpected end of file" error.

$(if [[ $firstRun == false ]]; then echo & fi)

Solution

  • You could write a wrapper function to conditionally run its arguments as a command in the background:

    conditionalbg() {
        if [ "$1" = true ]; then
            shift
            # run the passed command in the foreground
            "$@" &
        else
            shift
            # run the passed command in the foreground
            "$@"
        fi
    }
    
    ...
    runinbg=false
    for ...
        conditionalbg "$runinbg" curl "some curl options"
        runinbg=true
    end for loop