I have 4 scripts example script1,script2,script3,script4 .
I have to run script1,script2 and script3 parallely since this 3 scripts dont have dependencies .
Once script1,script2 and script3 got completed successfully , I have to trigger script4.
Can someone help me on this how to right script for this!!!
#!/bin/bash
./script1 &
./script2 &
./script3 &
Q=1
While (Q==1)
do
PID1=`ps -ef | grep -v grep | grep "script1"`
PID2=`ps -ef | grep -v grep | grep "script2"`
PID3=`ps -ef | grep -v grep | grep "script3"`
sleep 10 ## you can give here number of seconds as per you wish as not sure about how long scripts wil be taking to complete##
if [[ $PID1 == "" && $PID2 == "" && $PID3 == "" ]]
then
./script 4
Q=0
else
Q=1
fi
done
kindly try it in a non live environment as it is not tested and let us know.
EDIT: Sorry sea seems we have updated the post on same time didn't see your question.
The capture whether the script is running or not - is easy, at least when compared to the challenge transporting the exit value out of the subshell to that this handler script knows it, which script has exited with which exit code.
I would have a solution for this at hand, but I'm afraid it works only with 1 background process at a time (that is pseudo-copied to below example).
Either way, it forces you to send a more complex command in the background process, adapt to all 3 files.
( ./script1 ; printf $? > ./script1_ret )
Then catch the result of those ./script[1-3]_ret files within a loop, and it should be fine.
Hope this helps
EDIT:
Just for the challenge I've written a script, but not going to post until TO has posted his efforts.
If you don't care which ones fail, there is no need to keep track of the individual return codes at all:
./script1 & p1=$!
./script2 & p2=$!
./script3 & p3=$!
# Execute script4 only if all other scripts complete successfully:
wait $p1 && wait $p2 && wait $p3 && ./script4
rc=$?
# Wait for remaining children if script1 or script2 failed:
wait
exit $rc
Note that the shell is only required to keep track of CHILD_MAX processes and the standards say CHILD_MAX can be as small as 25. So, if you want to keep track of more than 25 background processes, verify that your shell can keep track of all of them by checking the CHILD_MAX limit on your system with the command:
getconf CHILD_MAX
Also note that if you have CHILD_MAX processes running, attempts to start any more processes (foreground or background) will fail.