0

Here is the nature of my problem:

I'm using my raspberry pi to run a wikipedia server, so I'd like to make a launcher (desktop icon) that starts up the wikipedia server and opens the web browser to localhost:8080. Whenever I'm writing a script to do this it will only do whichever one comes first. For example:

./wikipedia_server; ./web_browser

When they're separated by a semicolon it waits for the wikipedia server process to finish (which it never does), and then it launches the web browser. If i do

./web_browser; ./wikipedia_server

then it simply waits until i close the web browser.

Is there any way I could make a small script that would execute both of them at the same time, and if so how would i do it? Thanks.

1
  • Did you find a good solution to your question?
    – Jeff Schaller
    Commented Oct 8, 2015 at 11:06

1 Answer 1

2

You could send the wikipedia server to the background, run the web browser, then kill off the server when you're done:

./wikipedia_server &
server=$!
# wait for TCP port 8080 to open up
while ! netstat -tln | grep -q :8080; do :; done
./web_browser
kill $server

... on the assumption that you don't want the Wikipedia server running unless you are also browsing it.

(updated to add the idea from Ole Tange's comment: to wait for the web server's port to open before continuing)

5
  • This answers the question of running 2 processes at the same time. However whenever I have done this type of thing in the past, the web_browser will be up before the server, and I will get an error. If you can get the server to come up, then fork, then the parent process exit, then you will avoid this race problem. Commented Oct 3, 2015 at 23:19
  • A work-around there could be to add a sleep command before starting the browser; experiment with the times to see how long it takes.
    – Jeff Schaller
    Commented Oct 3, 2015 at 23:40
  • Using sleep can work, but will be slow and/or unreliable. Commented Oct 4, 2015 at 20:39
  • Look for a program listening on port 80: netstat -tln4c | perl -ne '/:80 / and exit'
    – Ole Tange
    Commented Oct 5, 2015 at 5:48
  • I updated my answer having now learned about netstat's "listen" and "continuous" flags -- thanks @OleTange!
    – Jeff Schaller
    Commented Oct 5, 2015 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .