Bobys
Bobys

Reputation: 677

bash wait for first python file to start before continue

I have this bash:

#!/bin/sh
# launcher.sh

echo "Remote Control Server is starting up..."
sudo python RControlPanel.py &
wait &
sudo python startup.py &
wait

The first python file is a flask server which its necessary to start up first. The second file is initialising the components on the raspberry pi, and turns on a couple of LEDs and stuff. The way the script is written requires the flask application first and then initialise the components. Its seems that the flask application it takes longer to start up and the bash script continues to run the startup.py

Is it possible to make sure that the flask app is running and then carry on to the next script? I though wait at the end will work but it doesnt. I have even tried with sleep.

Update: Im not quite sure but, I think when flask app runs, is getting to an endless loop, and waits for requests, like a normal web server do. Maybe thats the problem why the solutions bellow wont work.

Upvotes: 1

Views: 1967

Answers (3)

Bobys
Bobys

Reputation: 677

I came up with a solution which im using the Thomas answer. I have created a sh file which is called webserver.sh:

echo "Remote Control Server is starting up..."
sudo python RControlPanel.py

Then a second file which is called components.sh:

while ! curl http://127.0.0.1:80 -m1 -o/dev/null -s ; do
  sleep 0.1
  echo "Web Server still loading" #This line is for testing purposes
done
sudo python startup.py
echo "Startup Initialazation done. System Ready!"

And the a thrid file launcher.sh:

./launcher.sh
./remoteServer.sh

The first file starts up the web server only. no other code needs to be executed in there because it will be skipped, cause the flask app is an endless loop and it will skip everything underneath it.

The second file at the biggening is using the Thomas code to check if the webserver is running. If it does not is keep looping until the webserver (Flask app) come alive, and then run the startup.py python script which is initialising the components.

The third file is just calling launcher.sh and remoteServer.sh. So I can run my whole project within a single file, no matter which one is gonna start first.

Upvotes: 1

Martian
Martian

Reputation: 37

Just use:

sudo python RControlPanel.py && sudo python startup.py &

Double && ensures that the second command runs only after first returns exit status zero.

Upvotes: 0

Thomas
Thomas

Reputation: 181785

I suppose the Flask server opens some HTTP port? Let's say on port 8080, then you could poll the app like so:

while ! curl http://localhost:8080 -m1 -o/dev/null -s ; do
  sleep 0.1
done

Options:

  • -m1 to allow at most 1 second for the HTTP request. If your firewall is configured to silently drop packets to closed ports, this should make it go faster.
  • -o/dev/null so the HTTP response body doesn't get printed.
  • -s to hide any errors, as they are expected.

Add -S if you still want to see the "Connection refused" messages scroll by until the server is up.

Upvotes: 3

Related Questions