How To Run Parallel Jobs/Process/Programs in Bash – POFTUT

How To Run Parallel Jobs/Process/Programs in Bash


We generally run jobs in bash in serial order. Serial order means one command runs and after completion other command starts. There is another way to run jobs which are named parallel. Running parallel means multiple jobs run at the same time side by side.

Send Job To The Background

We will start with a simple example where two ping commands will be run in the background at the same time. We will add & to the end of each command. We will ping two domain names google.com and poftut.com.

$ ping google.com & ping poftut.com & 
Send Job To The Background 
Send Job To The Background

We can see from the output that after the command is issued we have two lines which show the commands processes and their related PID and there is also information about the background jobs in square brackets job id. Both commands will start in the background and will resume up to kill operation or exit.

Wait Before Running New Job

In some cases, we may need to wait before running multiple parallel jobs. We can use the bash wait function to wait for finish previously started jobs. In this example, we will run jobs j1 and j2 before running j3 . But we will wait to complete jobs j1 and j2 before running j3job in the background.

j1 &
j2 &

wait

j3 &

GNU Parallel

GNU Parallel is a very useful tool that can start multiple jobs in a parallel way. We will use the command parallel and specify the jobs we want to run after ::: . In this example, we will run ping poftut.com and ping google.com commands in parallel which will output the same time.

$ parallel ::: "ping poftut.com" "ping google.com"

How To Run Parallel Jobs/Process/Programs in Bash Infographic

How To Run Parallel Jobs/Process/Programs in Bash Infographic
How To Run Parallel Jobs/Process/Programs in Bash Infographic

LEARN MORE  How To Use Psexec Tools To Run Commands and Get Shell Remote Windows Systems?

Leave a Comment