Kevin
Kevin

Reputation: 671

How do you execute several bash functions in the background but wait for output?

I am trying to make a bash function to execute several web scrapes (using curl and such), and I want to have them all execute concurrently in the background, and all print their output to stdin. How is this possible?

Upvotes: 4

Views: 8244

Answers (2)

TrueY
TrueY

Reputation: 7610

It is an easy one! You can use & for this even for functions:

#!/usr/bin/bash

x() { echo =$1=Solaris; sleep 1; echo =$1=East; sleep 1; echo =$1=Panta Rhei;}

x one&
x two&

echo Syrius; sleep 1
echo After Crying 

Output:

=one=Solaris
Syrius
=two=Solaris
=one=East
=two=East
After Crying

Press ENTER or type command to continue
=one=Panta Rhei
=two=Panta Rhei

So x is running in the background and prints to stdout. The Press ENTER or type command to continue was presented by (as I started the script from ) and shows that the background x process finishes its job after the main script finished. If You want to wait until all the background processes finish You can use the $! to get the PID of the background processes and the built-in wait <PID> as a last function.

Upvotes: 8

Kevin
Kevin

Reputation: 671

For anyone else who comes along:

Given the following:

function par() {
    parallel sh -c ::: "sleep 1" "sleep 1" "sleep 1"
}

function seq() {
    sleep 1
    sleep 1
    sleep 1
}

Timing both commands shows that the first function executes all at once while the second function waits for each command to be finished before moving on to the next:

$ time par && time seq

real    0m1.215s
user    0m0.147s
sys     0m0.085s

real    0m3.028s
user    0m0.011s
sys     0m0.012s

Happiness!

Upvotes: 2

Related Questions