Linux – Run multiple background processes through shell scripts

Run multiple background processes through shell scripts… here is a solution to the problem.

Run multiple background processes through shell scripts

I’m new to shell scripting, so please forgive me for my lack of knowledge.

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

Step 1:

  • Start server1 (have it running in the background).

  • Run some commands (custom commands) on this server

Step 2:

  • Start Server 2

Step 3:

  • After starting server1 and server2, run my python script and display its output on the terminal

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 doesn’t work at all.
I tried removing serverCommand1 and serverCommand2 and this works, but the python script doesn’t wait for server2 to start.

The terminal also displays the output of server1 and server2 instead of the output of the python script.

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

Solution

The && in the script looks a bit confusing. Record:

  • Commands running in the background are followed by an &
  • && Used to chain multiple commands on success, e.g. cmd1 && cmd2 will execute cmd1 and only on successful exit, it will execute cmd2. Both commands will run in the foreground, where there is no background 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

In fact, instead of sleeping for a fixed amount of time, it would be better to detect that the server is ready and react to it. For example, in the log, there might be a message that the server is ready, suppose a message displays “READY”. Then you can do 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 where it sleeps for 5 seconds and checks if the log contains the text “READY” in each sleep. If so, end the loop. You can come up with a variant that suits your needs.

Related Problems and Solutions