Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parallel call multiple bash functions

I have read the example at http://www.gnu.org/software/parallel/man.html#example__calling_bash_functions however, is it possible to use gnu parallel to call 2 functions which do not have any variables you pass to them ?

example

a() {
  echo "download a"
  wget fileA
}

b() {
  echo "download b"
  wget fileB
}

and use parallel to call both functions a & b ?

like image 480
p4guru Avatar asked Dec 21 '15 13:12

p4guru


People also ask

What is || in bash script?

Logical OR Operator ( || ) in Bash It is usually used with boolean values and returns a boolean value. It returns true if at least one of the operands is true. Returns false if all values are false.

How run multiple processes in Linux?

In case you need to execute several processes in batches, or in chunks, you can use the shell builtin command called "wait". See below. The first three commands wget commands will be executed in parallel. "wait" will make the script wait till those 3 gets finished.


2 Answers

Run them in background. And then wait for them to complete.

a() {
  echo "download a"
  wget fileA
}

b() {
  echo "download b"
  wget fileB
}

a &
b &
wait # waits for all background processes to complete
like image 85
Thirupathi Thangavel Avatar answered Sep 17 '22 07:09

Thirupathi Thangavel


If you insist on using GNU Parallel:

a() {
  echo "download a"
  wget fileA
}

b() {
  echo "download b"
  wget fileB
}
export -f a
export -f b
parallel ::: a b

If you need read access to variables in the shell you can either export the variables or use env_parallel.

like image 44
Ole Tange Avatar answered Sep 18 '22 07:09

Ole Tange