I am looking for projects to work on
Please contact with me at!

Saturday, February 9, 2013

shell script to block main script exit before all other spined backend script exit.

Sometimes, we need to write shell script to spin multiple process running parallel. Also, we want to wait all parallel process finish before we can process to next step. How to achieve this? Below is a simple Bash shell sample script to do this.

for ((i=1; i < $n; i++));
 #compose the command
 realCmd="nohup yourCmdWithArguments >/dev/null 2>&1 &"
 #excute the command
 eval $realCmd
 #store command pid in an array.

#wait for each process to be done
for ((i=1;i<$n;i++));
   wait ${pidArray[$i]}

#continue to do next step that depends on results of all nohup spin processes above.

No comments:

Post a Comment