0

I am fairly new in bash scripting. I need to use bash script for manage my microservices, so these services need build and deploy(on docker).

So I have commands like that :

#!/bin/bash
mvn clean
wait $!
mvn package
wait $!
#Some if elses here
sudo docker build -t myService . &
wait $!
#Some if elses here
sudo -S docker run -d -name myService myService &

So as you see(a little part of script, i am grouping depended commands in functions),commands need to wait previous depended commands like that. But wait $! command doesn't look nice after all command.

Are there any approaches for this situations?(I am open other approaches instead of bash scripting if it is handy solution. Btw services are node and java services seperated, so I need to start all of them with one bash script.)

1 Answer 1

1

To automatically wait for a command, just don't add a & to them:

sudo docker build -t myService .
sudo -S docker run -d -name myService myService

This waits for sudo docker build -t myService . to completely exit before continuing with sudo -S docker run -d -name myService myService. It then waits for sudo -S docker run -d -name myService myService to completely exit before continuing with anything else.

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

5 Comments

Thanks for your answer, It's okay for maven commands but for docker build and run commands it needs wait, I just wonder do I have to use wait $! for depended processes per line.
@İlkerKorkut I updated the example to use the docker commands instead of the mvn commands. They work exactly the same way, but hopefully this is more clear.
Btw I am using & notation for some background server jobs is it correct for background situations?, so in docker commands its my bad. Also I couldn't realize wait $! when need it exactly?
You only need it if you want to parallellize commands for efficiency. For example, you can do gzip backup_alice.tar & gzip backup_bob.tar; wait $!; scp backup_*.tar.gz host:/backups to compress two files at the same time on a multicore system
Thanks for clarification.

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.