1

I'd like to automatically start all of the following applications after the system startup. However, they need to be started in a specific order and when the previous application is done booting.

The commands I'm now performing manually in the terminal are:

sudo docker-compose up
java -jar ./myapp.jar
python3 ./myapp.py
npm start ./myapp
chromium-browser www.my.app

So my java app needs to wait for the docker container to be started, otherwise it will fail to start. Analogically, the python app needs to wait for the java app to be started, and finally the web page needs to be opened after the angular frontend is ready to be served. Is there some easy way, how to handle this by a bash script, without getting way too deep with some DevOps tools? It's just a small pet project and I don't want to spend way too much time by over-engineering it.

2 Answers 2

1

Great, I was going to make a big disclaimer, but you have the right thing in mind, so I'll make a small one for people that come by and don't have the time to read it all thoroughly.

** NOT FOR PRODUCTION **

  1. This will be a cycle of starting an app and waiting for a condition. In order for your script to progress from one command to the next, you will need to start all the applications in the background.

So, in a nutshell

~ start app & #don't wait for the script to end
~ some waiting script # runs until condition passes

Here's a good example of a waiting script: https://github.com/vishnubob/wait-for-it

In a nutshell what this wait-for-it script does, you can implement manually by creating a while loop that runs until a timeout or until some condition is valid.

Examples of valid conditions:

  • status 200 on localhost on port 8000

You could implement these bash functions manually, it's basically a while loop and if and a counter, but I'll assume you don't want to waste time on it and I'll make an example script using the wait-for-it.sh

So, here it is:

#! /bin/bash

sudo docker-compose up -d # -d will start the containers in detached mode
./wait-for-it.sh localhost:12345 #replace 12345 with the correct port
java -jar ./myapp.jar & # &sign, sends the app to the background
./wait-for-it.sh localhost:12345
python3 ./myapp.py &
./wait-for-it.sh localhost:12345
npm start ./myapp &
./wait-for-it.sh localhost:12345
chromium-browser www.my.app

#this is kill all background processes when you quit this script
trap 'kill $(jobs -p)' EXIT 

#this will wait for all the background processes to quit before letting this script exit
wait -n 

1
  • 1
    That helps! Thanks a lot! Commented Nov 19, 2021 at 8:37
2

If you're already launching one of these services with Docker Compose, I think it would be a very small lift to Dockerize the remaining Java, NodeJS, and Python components. When you have built a docker image for each one, then they can be configured to start in the correct order by using depends_on in your compose file. Then you can bring them all up in the correct order by just running docker-compose up.

you can sleep for a bit before launching the browser, or monitor the stdout of the last service to start for a good "ready" message before launching chromium.

1
  • that's actually really neat and easy solution Commented Nov 19, 2021 at 8:37

Your Answer

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

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