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?
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With