Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 628
Kevin Avatar asked Dec 27 '22 01:12

Kevin


2 Answers

It is an easy one! You can use & for this even for bash 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 vim (as I started the script from vim) 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.

like image 115
TrueY Avatar answered Jan 13 '23 15:01

TrueY


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!

like image 23
Kevin Avatar answered Jan 13 '23 14:01

Kevin