background process return code

Hi
I have the following piece of code that is calling another child process archive.ksh in the background

while read file;
do
file_name=`ls $file`;
ksh archive.ksh $file_name &;
done < $indirect_file

The problem is, indirect_file may contain anwhere from 2 to 20 different filenames and archive.ksh takes only one file name at a time (that is a limitation). So based on the number of files in indirect_file, i shall run those many background processes for archive.ksh.

I need to check for the completion of all archive.ksh jobs that i fired in the background and only if all the bg jobs are successful, i need to make this process pass otherwise I abort it.

I found that there is a wait command that will wait for bg jobs to finish..Can I use it? if yes, how do i overcome following prob :

  1. How do I specify the job id or pid to wait command when I dont know it beforehand?

  2. I have heard that there is a restriction on the number of jobs you can fire? What is the number?

Any ideas??

Thanks

start with something like this:

#!/bin/ksh

>logfile

echo "start time `date`" > logfile

while read file
do
    file_name=`ls $file`;
    (ksh archive.ksh $file_name; echo "$file_name status=$?">> logfile)&
done < $indirect_file
wait

echo "end time `date`" >> logfile
cat logfile

I have not tested this...

Jim

Thanks for your reply

Per your code, does "wait" wait for all the bg jobs? I had heard there was some kind of upper limit.

Thanks

Jim

Once again, thanks for your script. I tested it out and found out that there is a upper limit of 25. Contrary to what I thought, it looks like 25 is not the limit of "wait" but apparantly it is the limit on number of background job I can submit.

If I want to run the archive script for even 26 times, it gives me following error:

bg.ksh[6]: cannot fork - try again

I guess this is some sort of limit set by sysadmin??

Anyways, for the time being, i guess that would serve my purpose.

Thanks again

The limit is usually a system parameter.
if your limit is 25 try this:

#!/bin/ksh

>logfile

echo "start time `date`" > logfile
let counter=0
while read file
do
    file_name=`ls $file`;
    (ksh archive.ksh $file_name; echo "$file_name status=$?">> logfile)&
    let counter=$counter+1
    if [ `echo "$counter%25" | bc` -eq 0 ] ; then
          wait
    fi
done < $indirect_file
wait

echo "end time `date`" >> logfile
cat logfile

Thats is great and good idea .. hope it will work...