1

I am running a simple shell script which has a for loop and kicks off 16 threads in the back ground and waits for each of them to finish before moving on the next commands.

Here is the shell:

total_threads=16

    echo "" 
    echo "Running Web Exclusive Item updates ... "  
    echo "Total Number of threads = " $total_threads
     for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
     do
        echo "Running Thread $i ..  "
        ./run_upd_web_excl_items.sh $i $total_threads &
     done
     wait
     echo "Done."


    echo ""  
    echo "Committing data ... "
    output_val=`$ORACLE_HOME/bin/sqlplus -S $UP <<endofsql
       whenever sqlerror exit 2
       whenever oserror exit 3
       SET HEADING  OFF
       SET FEEDBACK OFF
       SET ECHO     OFF
       set serverout on
      commit;
      /
    exit
    endofsql`
    echo "Done."

As I was running this, the output is coming as follows:

~> . ./run_upd_web_excl_items_mitctrl.sh

Running Web Exclusive Item updates ...
Total Number of threads =  16
Running Thread 1 ..
Running Thread 2 ..
Running Thread 3 ..
Running Thread 4 ..
Running Thread 5 ..
Running Thread 6 ..
Running Thread 7 ..
Running Thread 8 ..
Running Thread 9 ..
Running Thread 10 ..
Running Thread 11 ..
Running Thread 12 ..
Running Thread 13 ..
Running Thread 14 ..
Running Thread 15 ..
Running Thread 16 ..
Total time for Thread 3 : 0 minute(s), 22.5 second(s).
Total time for Thread 2 : 0 minute(s), 23.3 second(s).
Total time for Thread 12 : 0 minute(s), 24.3 second(s).
Total time for Thread 8 : 0 minute(s), 24.8 second(s).
Total time for Thread 7 : 0 minute(s), 29.9 second(s).
Total time for Thread 1 : 0 minute(s), 30 second(s).
[1]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[2]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[3]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[7]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[8]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[12]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 16 : 0 minute(s), 32.1 second(s).
Total time for Thread 4 : 0 minute(s), 32.8 second(s).
[4]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[16]+  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 10 : 0 minute(s), 33.2 second(s).
Total time for Thread 13 : 0 minute(s), 33.7 second(s).
Total time for Thread 5 : 0 minute(s), 33.8 second(s).
[5]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[10]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[13]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 14 : 0 minute(s), 35.6 second(s).
Total time for Thread 6 : 0 minute(s), 36.8 second(s).
[6]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[14]-  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 11 : 0 minute(s), 37.7 second(s).
Total time for Thread 9 : 0 minute(s), 37.8 second(s).
[9]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[11]-  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 15 : 0 minute(s), 38.8 second(s).
[15]+  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Done.

Committing data ...
Done.

Normally, I would have expected an output like this:

~> ./run_upd_web_excl_items_mitctrl.sh

Running Web Exclusive Item updates ...
Total Number of threads =  16
Running Thread 1 ..
Running Thread 2 ..
Running Thread 3 ..
Running Thread 4 ..
Running Thread 5 ..
Running Thread 6 ..
Running Thread 7 ..
Running Thread 8 ..
Running Thread 9 ..
Running Thread 10 ..
Running Thread 11 ..
Running Thread 12 ..
Running Thread 13 ..
Running Thread 14 ..
Running Thread 15 ..
Running Thread 16 ..
Total time for Thread 1 : 0 minute(s), 26.5 second(s).
Total time for Thread 10 : 0 minute(s), 27.1 second(s).
Total time for Thread 2 : 0 minute(s), 27.5 second(s).
Total time for Thread 6 : 0 minute(s), 27.9 second(s).
Total time for Thread 3 : 0 minute(s), 27.9 second(s).
Total time for Thread 15 : 0 minute(s), 27.9 second(s).
Total time for Thread 9 : 0 minute(s), 28 second(s).
Total time for Thread 5 : 0 minute(s), 28 second(s).
Total time for Thread 16 : 0 minute(s), 28.1 second(s).
Total time for Thread 8 : 0 minute(s), 30.5 second(s).
Total time for Thread 12 : 0 minute(s), 31 second(s).
Total time for Thread 11 : 0 minute(s), 31.5 second(s).
Total time for Thread 7 : 0 minute(s), 31.9 second(s).
Total time for Thread 14 : 0 minute(s), 32 second(s).
Total time for Thread 13 : 0 minute(s), 32.7 second(s).
Total time for Thread 4 : 0 minute(s), 34.8 second(s).
Done.

Committing data ...
Done.

I am not able to figure out why the shell is printing the extra lines like :

[13]   Done                    ./run_upd_web_excl_items.sh $i  $total_threads

Any thoughts?

2

It’s because you are running the script with . script_name.  The shell (your main, interactive shell) is treating the commands in this file as if you were typing them manually at the shell prompt.  The messages that you are seeing are the ones that the shell normally displays when a primary background process (i.e., one launched directly from the main shell) terminates.

Unless you have some particular reason for doing it this way, I suggest that you run the script either with

sh run_upd_web_excl_items_mitctrl.sh

(or use bash if you need to), or make it executable with

chmod +x run_upd_web_excl_items_mitctrl.sh

and then run it with

./run_upd_web_excl_items_mitctrl.sh
2

The reason is because they're jobs being run in the background of your current shell.

When you use . or source to run a script then it's not run in a subshell, it's exactly as if you typed the commands directly in to your terminal.

Add #!/bin/bash as the first line of your script and run it directly instead of sourcing it.

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.