Some commands inside unix script are not working

Viewed 25

I have invoke_test.sh with below code:

echo "invoke script started"
sh test1.sh >output1.log &
sh test2.sh > output2.log
echo "test 1 completed " >> output1.log
echo "test 2 completed " >> output2.log

test1.sh:

echo "running test1.sh"
sleep 5
echo "test1.sh completed"

test2.sh:

echo "running test2.sh"
    sleep 10
    echo "test2.sh completed"

So when I execute invoke_test.sh by sh invoke_test.sh

iN output log files i can find eg output1.log

running test1.sh test1.sh completed

but not able to get the below line which i am trying to direct to log from invoke_test.sh script test 1 completed

The requirements here is to run 2 shell scripts parallely and once both the execution completes I want to execute these 2 lines echo "test 1 completed " >> output1.log echo "test 2 completed " >> output2.log

I am new learner in unix so will appreciate why this behaviour is happening in a simple way and how can I achieve the desired output

1 Answers

If your goal is to wait until all the background processes are done, use wait:

echo "invoke script started"
sh test1.sh > output1.log &
sh test2.sh > output2.log &
wait   # wait for both of the background jobs to complete
echo "test 1 completed " >> output1.log
echo "test 2 completed " >> output2.log
Related