How to make asynchronous function calls in shell scripts How to make asynchronous function calls in shell scripts shell shell

How to make asynchronous function calls in shell scripts


Something to experiment with:

delayed_ajax() {  local url=$1  local callback=$2  local seconds=$3  sleep $seconds  curl -s "$url" | "$callback"}my_handler() {  # Read from stdin and do something.  # E.g. just append to a file:  cat >> /tmp/some_file.txt}for delay in 120 30 30 3000 3000; do  delayed_ajax http://www.example.com/api/something my_handler $delay &done


You can also use the & symbol to put this task into the background:

sleep 14 && wget http://yoursitehere.com &sleep 18 && wget http://yoursitehere.com &sleep 44 && wget http://yoursitehere.com &

This creates a background task that sleeps for a fixed amount of time, then runs the command. It doesn't wait for each of the tasks to finish.

&& here means that if the previous thing completes without an error, do the next thing.