paint-brush
Boil your eggs in parallel in BASHby@kavehmz
679 reads
679 reads

Boil your eggs in parallel in BASH

by Kaveh Mousavi ZamaniMay 4th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We are in an era which our machines have many cores. We also have languages which can use those cores in simple ways.

Company Mentioned

Mention Thumbnail
featured image - Boil your eggs in parallel in BASH
Kaveh Mousavi Zamani HackerNoon profile picture

We are in an era which our machines have many cores. We also have languages which can use those cores in simple ways.

Imagine you want to boil eggs in parallel in a new language like Go:

var wg sync.WaitGroup





func boilAnEgg(i int) {time.Sleep(time.Second)fmt.Println("Your egg is ready: ",i)wg.Done()}








func main(){wg.Add(10)for i:=1;i<=10;i++ {go biolAnEgg(i) # Adding "go" here simply will run the function as a goroutine here}wg.Wait()fmt.Println("All eggs are ready")}

But what about our 40 years old bash?

Lets boil our eggs in parallel in bash.




boil_an_egg() {sleep 1printf "Your egg is ready: $1\n"}






for i in $(seq 1 10)doboil_an_egg $i &donewaitprintf "All eggs are ready\n"

Adding “&” at the end of your command will push it into background to run in parallel.

The command “wait” waits for all your background processes to finish.

It is that simple.

As an example when I needed to clone around 40 github repositories for some reason in travis-ci, only using this method, I was able to bring down the cloning time from 120 seconds to around 18 seconds.

This is how simple it was:








OUT=$(mktemp)for i in garbage queue primedo(git clone [email protected]:kavehmz/$i.git || echo "cloning failed for: $i" >> $OUT ) &donewait[ -s $OUT ] && cat $OUT && exit 255echo "Git repositories are ready"

Bonus

List the running jobs:



sleep 10 &sleep 15 &jobs


[2] + Running sleep 15[1] - Running sleep 10

Next notice first one is not in the list! You can use braces to your advantage:



(sleep 10 &)sleep 15 &jobs

[1] + Running sleep 15

But this time first job is included!:



(sleep 10) &sleep 15 &jobs


[2] + Running sleep 15[1] - Running sleep 10

Now lets wait only for the second job to finish (%2):




sleep 10 &sleep 5 &sleep 15 &wait %2

[2] - Done sleep 5

Of course you can’t go too far with bash but as a simple tool which is ready for you in many places with no effort or installation you can use it to your advantage for many tasks.

Read more at: https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!