3

I'm new to shell scripting, so pardon my lack of knowledge.

My aim is to run two servers server1 and server2 in the background and then run a python script scriptRun through my shell script.

Step1:

  • Launch server1 (making it run in the background)

  • Run a few commands on this server (which are customized commands)

Step2:

  • Launch server2

Step3:

  • Run my python script and display its output on the terminal after server1 and server2 are launched

My shell script looks like this:

echo "Launching server1"
java StartServer1.jar && (serverCommand1 && serverCommand2) &

echo "Launching server2"
java StartServer2.jar &&

echo "Running script"
python scriptRun.py

This script does not work at all. I tried removing the serverCommand1 and serverCommand2, this works, but the python script does not wait for server2 to launch.

Also the terminal displays the outputs of server1 and server2, and not the output of the python script.

My question is how do I run multiple processes in the background and run another process which is dependent on the previous processes?

2 Answers 2

3

The && in your script seem a bit confused. For the record:

  • Put a single & after a command to run it in the background
  • && is for chaining multiple commands if successful, for example cmd1 && cmd2 will execute cmd1 and only if it exits with success, it will execute cmd2. Both commands will run in the foreground, there is no backgrounding here at all.

Maybe you want to do something like this:

echo "Launching server1"
java StartServer1.jar >server1.log 2>server1.err
sleep 5  # give some time for the server to come up

serverCommand1
serverCommand2

echo "Launching server2"
java StartServer2.jar >server2.log 2>server2.err
sleep 5  # give some time for the server to come up

echo "Running script"
python scriptRun.py

Actually, rather than sleeping for a fixed amount of time, it's better if you can detect that the server is ready and react on that. For example in the logs, maybe there is a message indicating that the server is ready, let's say a message that says "READY". Then you can do something like this:

echo "Launching server1"
java StartServer1.jar >server1.log 2>server1.err
while :; do sleep 5; grep -q READY server1.log && break; done

That's an infinite loop there, in every sleep it sleeps for 5 seconds and checks if the log contains the text "READY". If it does it ends the loop. You can come up with a variation of this that suits your needs.

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

4 Comments

Thank you! That was very helpful. I think i'll build up on sleep and try something else with that.
@user2354302 I improved my answer
Thank you. That's more flexible My serverCommand1 and 2 dont work. It says command not found. Will have to find a work around for that.
Rather than sleeping an arbitrary time and polling by re-reading the whole file with grep, you can process the file as it is written with something like tail -f -n +1 server1.log | grep -q READY. The tail -f will follow the file as it is written to; the -n +1 says to start from the beginning (in case the READY has already been written when it starts); and then it is piped into the same grep -q command to return successfully when READY is found. This is less demanding, and will continue as soon as possible, rather than with up to a 5 second delay.
1

You need to wait for the background processes to complete before executing your python script. Additionally, redirect STDOUT and STDERR from the java processes to /dev/null if you want to ignore those:

echo "Launching server1"
java StartServer1.jar >/dev/null 2>&1 &

echo "Launching server2"
java StartServer2.jar >/dev/null 2>&1 &

wait     # wait for the background processes to complete

echo "Running script"
python scriptRun.py

(I'm not sure how your server commands work, but you might need to wait after starting server1 if you need to issue commands to it.)

1 Comment

Thanks for the quick reply. The wait command is possibly waiting for server2 to complete or exit. I want it to wait till its launched and then run my python script. Your script successfully redirects the output, but the python script never runs, maybe its waiting for server1 and 2 to be killed.

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.