0

Hi I have a script mainscript.sh In main script I have multiple chile script.

child1.sh  
child2.sh  
child3.sh 
rm -rf /home/bdata/batch/* 

I am running my mainscript.sh which will run all child jobs in parallel.

mainscript.sh

child1.sh &
child2.sh &
child3.sh &
rm -rf /home/bdata/batch/*   

4th statement runs before completing all the execution.

Is there any way I can control the execution of last line after finishing above 3 scripts in parallel.

Amaresh
  • 3,231
  • 7
  • 37
  • 60
  • 1
    Add & at the end for each script. If you want to run irrespective of terminal then go for nohup option. – Nachiket Kate Jan 29 '15 at 12:17
  • Perl supports multithreading and forking if that's an acceptable solution. I know it's not quite shell, but is nearly as ubiquitous: http://stackoverflow.com/questions/26296206/perl-daemonize-with-child-daemons/26297240#26297240 – Sobrique Jan 29 '15 at 13:08
  • @Kate --That works but the problem is I want to run the last line after all other shell script finished execution.In my case it is deleting my folder before finishing all the script – Amaresh Jan 30 '15 at 07:14

2 Answers2

2

This is one solution:

#!/bin/bash
./child1.sh &
./child2.sh & 
./child3.sh &

Use ./ or full path to the script.

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. If you want to execute all child-scripts before exiting the script then add wait at the end (as @Mark Setchell wrote below).

user1766169
  • 1,932
  • 3
  • 22
  • 44
2

Simply tell the shell to wait until the children are all dead:

child1.sh &
child2.sh &
child3.sh &
wait
rm -rf /home/bdata/batch/*  
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278