I have a main bash script that executes several smaller bash scripts (and each smaller script runs a docker container) But I want the 2nd script to run only after the first has finished executing, and so on. That is, I want the processes of each script to NOT run simultaneously. Example:

1st_script.sh param1 param2 param3
2nd_script.sh param1 param2

Suggestions online are not working for me. I tried to get PID of 1st_script with:

1st_Script & echo $!

to then use ps -p <PID> || <2nd_script> But although <1st_Script> & echo $! gives a PID, when I check if process is there with ps -p <PID> there's nothing. I know 1st_script is running cause the docker container it runs is listed with docker ps. So it's process should also be listed with ps -p <PID>

1st_Script && 2nd_Script && 3rd_Script && ... is not a option cause I have a lot of scripts and is not readable


Continuation lines can greatly improve readability:

script1 && script2 && script3 && scriptn


script1 \
  && script2 \
  && script3 \
  && scriptn

But I'm having trouble reproducing your problem. What gives you the impression that your scripts are not running sequentially?

I made two test scripts that each contained

echo hello
sleep 15s

and then a script that contained only


and measured the wait using time to get the following output


real    0m30.003s
user    0m0.002s
sys     0m0.001s

If the scripts ran concurrently, the total time taken would have been less than 30 seconds.

Is your issue instead that you want the container to stop running before moving to the next script?

  • Yes, want the container to stop/finish running before moving to the next script. At the end, it worked by doing: dockerId=$(docker command) and then docker wait $dockerId. This inside each small script. Also worked by just removing the '-d' flag from docker run command
    – salamandra
    Apr 22 '20 at 17:10
  • If you can, make sure to answer your own question with sample code and mark it answered so others can find your solution too!
    – dcwaters
    Apr 23 '20 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.