tags:

views:

612

answers:

3

I have 1 bash script that runs another bash script, however the first bashscript isn't waiting for the second one to complete before proceeding, how can I force it to wait?

For example:

#!/bin/bash
# first.sh

#call to secondary script
sh second.sh
echo "second.sh has completed"

echo "continuing with the rest of first.sh..."

The way it is now, it will run second.sh, and continue on, without waiting for second.sh to complete.

+4  A: 

Normally it does; something else is happening. Are you sure that the other script isn't running something in the background instead? You can try using wait regardless.

Ignacio Vazquez-Abrams
'wait' only waits for direct children of the parent bash; if the child runs something in the background and exits, the parent process cannot wait for it because it is inherited by 'PID 1' - the init process. (Unless something has changed dramatically that I don't know about.)
Jonathan Leffler
A: 

try using bash second.sh and check your second.sh and make sure you don't have programs that run in the background

ghostdog74
+1  A: 

You can simply add the command wait after you execute the second script, it will wait for all process that you launch from your principal script

You can even recuperate the PID of your second script using the command echo $! directly after you call the second script, and then pass this PID as an argument to the wait command