Running multiple background processes through shell scripting
background-process, bash, linux, shell
Solution
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.
Problem
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?