If you working as a Linux administrator or as a DevOps engineer then you might get a use case where you need to run multiple bash scripts in parallel without waiting to finish execution of previous script. Let me show you possible ways to achieve this in bash script

For e.g I have 5 bash scripts which I would like to execute in parallel.

  • taskscript1.sh
  • taskscript2.sh
  • taskscript3.sh
  • taskscript4.sh
  • taskscript5.sh

Method -1 : using nohub command with &

nohup bash taskscript1.sh &
nohup bash taskscript2.sh &
nohup bash taskscript3.sh &
nohup bash taskscript4.sh &
nohup bash taskscript5.sh &

Or you can use for loop like below

for((i=1;i<5;i++)); do nohup bash taskscript${i}.sh & done

When nohup command use with ‘&’ then it doesn’t return to shell command prompt after running the command in the background.

You can also execute all bash script of a directory using below command.

for script in dir/*.sh
do
    nohup bash "$script" &
done

Method – 2: Use GNU Parallel Tool

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables. A job can also be a command that reads from a pipe. GNU parallel can then split the input and pipe it into commands in parallel.

parallel -j0 bash :::: <(ls taskscript{1..5}.sh)

 

(Visited 6,474 times, 18 visits today)