2

said i have looping script in file test.sh, i just want to running script "running_script2.sh" after the background process is complete, what i try like this, but it's not working .

for id in 1 2 3
do
bash running_script1.sh $id &   (#complete 1 minute/looping)
done
wait
echo "running script2 ..."
bash running_script2.sh

its work when :

bash running_script1.sh 1 &
bash running_script1.sh 2 &
bash running_script1.sh 3 &
wait
echo "running script2 ..."
bash running_script2.sh

but its not efficient

  • The first line of your example, the for statement, should have the literal word in between id and the numerals. – user1404316 Aug 13 '18 at 3:59
  • ups, I forgot to add 'in' – Anggoro Setiawan Aug 13 '18 at 4:10
1

There is no semantic difference between

for id in 1 2 3
do
bash running_script1.sh $id &   (#complete 1 minute/looping)
done

and

bash running_script1.sh 1 &
bash running_script1.sh 2 &
bash running_script1.sh 3 &
wait
echo "running script2 ..."
bash running_script2.sh

This demonstration script below runs little over 3s (you can time it) regardless of whether the condition command is true or false:

#!/bin/sh -eu
for i in 1 2 3; do
    echo "sleep $i" > $i && chmod +x $i
done
if false; then
    for i in 1 2 3; do
        bash ./$i &
    done
else
    bash ./1 &
    bash ./2 &
    bash ./3 &
fi
wait

If you're still experiencing discrepancies, please post an mcve.

0

Don't know whether this is a right approach but I have used screen to run script
screen is a virtual terminal to run process in bg

for id in 1 2 3
do
screen -d -m bash script1.sh $id
done
wait
echo "running script2 ..."
bash script2.sh

screen -d -m will run the script and closes the screen after the script completes.
I cannot get the output of script1 in standard output while using screen so I used a file to store the output of script1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.