Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel processing or threading in Shell scripting

Tags:

shell

unix

I am writing a script in shell in which a command is running and taking 2 min. everytime. Also, there is nothing we can do with this. But if i want to run this command 100 times in script then total time would be 200min. and this will create a big issue. Nobody want to wait for 200min. What i want is to run all 100 commands parallely so that output will come in 2min or may be some more time but dont take 200min.

it will be appreciated, if any body can help me on this in any way.

like image 893
Prince Garg Avatar asked Jun 18 '12 16:06

Prince Garg


People also ask

What is multithreading in shell script?

Parallel executes Bash scripts in parallel via a concept called multi-threading. This utility allows you to run different jobs per CPU instead of only one, cutting down on time to run a script.

How do I run a parallel process in shell script?

Examples. To download all files in parallel using wget: #!/bin/bash # Our custom function cust_func(){ wget -q "$1" } while IFS= read -r url do cust_func "$url" & done < list. txt wait echo "All files are downloaded."

Which shell is best for scripting?

Tenex Shell This shell is recommended for programmers based on its C-like syntax, as those developers can use scripting features without any experience in sh or Bash.

How many types of shell scripts are there?

Shell scripts - sh, bash, csh, tcsh. Other scripting languages - TCL, Perl, Python.


2 Answers

GNU Parallel is what you want, unless you want to reinvent the wheel. Here are some more detailed examples, but the short of it:

ls | parallel gzip # gzip all files in a directory
like image 165
jmdeldin Avatar answered Nov 07 '22 07:11

jmdeldin


... run all 100 commands parallely so that output will come in 2min

This is only possible if you have 200 processors on your system.

There's no such utility/command in shell script to run commands in parallel. What you can do is run your command in background:

for ((i=0;i<200;i++))
do
   MyCommand &
done

With & (background), each execution is scheduled as soon as possible. But this doesn't guarantee that your code will be executed in less 200 min. It depends how many processors are there on your system.

If you have only one processor and each execution of the command (that takes 2min) is doing some computation for 2 min, then processor is doing some work, meaning there's no cycles wasted. In this case, running the commands in parallel is not going help because, there's only one processor which is also not free. So, the processes will be just waiting for their turn to be executed.

If you have more than one processors, then the above method (for loop) might help in reducing the total execution time.

like image 32
P.P Avatar answered Nov 07 '22 09:11

P.P