Here is a script written for bash:
#!/bin/bash sleep 1 & sleep 2 & sleep 3 & for t in {10..0} do # jobs echo "Waiting("$t")..." JCNT=`jobs | wc -l` echo "JCNT="$JCNT if [ $JCNT -eq 0 ]; then break fi sleep 1 done exit 0 It works for 10 seconds, but not for 3 seconds as expected. But if to uncomment the "jobs" command then it works fine. I don't have any idea why. Can you help me?
The script output with "jobs" commented:
Waiting(10)... JCNT=3 Waiting(9)... JCNT=2 Waiting(8)... JCNT=1 Waiting(7)... JCNT=1 Waiting(6)... JCNT=1 Waiting(5)... JCNT=1 Waiting(4)... JCNT=1 Waiting(3)... JCNT=1 Waiting(2)... JCNT=1 Waiting(1)... JCNT=1 Waiting(0)... JCNT=1 And with uncommented "jobs" command:
[1] Running sleep 1 & [2]- Running sleep 2 & [3]+ Running sleep 3 & Waiting(10)... JCNT=3 [1] Done sleep 1 [2]- Running sleep 2 & [3]+ Running sleep 3 & Waiting(9)... JCNT=2 [2]- Done sleep 2 [3]+ Running sleep 3 & Waiting(8)... JCNT=1 [3]+ Done sleep 3 Waiting(7)... JCNT=0 ADDED
I modified the script this way:
#!/bin/bash sleep 1 & sleep 2 & sleep 3 & for t in {10..0} do KKK=`jobs` echo $KKK echo "Waiting("$t")..." JCNT=`jobs | wc -l` echo "JCNT="$JCNT if [ $JCNT -eq 0 ]; then break fi sleep 1 done exit 0 ...and found that there is info about the last job which is done:
[3] Done sleep 3 Calling "jobs" simply purges the list of done jobs. Doing "jobs|wc -l" is the wrong way to count active jobs. The right way is "jobs -r|wc -l".