0

I am unable to execute 2 scripts in parallel through shell script.

I have 3 scripts: script1.sh, script2.sh and installpackages.sh.

My requirement is script1.sh and script2.sh have to run in parallel in the background and after completion of script1.sh and script2.sh, installpackages.sh should be executed.

selid1t601.xayybol.74> vi script.sh
exec script1.sh &
exec script2.sh &

installpackages.sh

can you please suggest how can i execute script1.sh and script2.sh in parallel?

2 Answers 2

1

Since you are already executing the scripts in background those are essentially running in parallel. The only thing you need is to wait to ensure that the scripts have been executed before proceeding to the final step. You need to say:

exec script1.sh &
exec script2.sh &
wait
installpackages.sh

From the manual:

wait

   wait [jobspec or pid ...]

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.

Sign up to request clarification or add additional context in comments.

Comments

1

don't do exec in this case. this replaces the current process with the started program. just run them like

bash script1.sh &
bash script2.sh &

or

./script1.sh &
./script2.sh &

and put a wait behind as devnull said.

1 Comment

Using exec reduces the number of processes required. ./script1.sh & first forks a new shell, then that shell forks a process for script1.sh to run in. exec ./script1.sh & forks a new shell, then that shell is replaced by the process for script1.sh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.