bash tee and background threads

Running centos 2.6, I have a bash script in which I'd like to run a number of background threads in parallel, tee'ing the results of the entire script to one file, while tee'ing the result of each background thread to another.

Here's what I'm doing, where the number of csv files control the number of background threads.

cat test.sh

#!/bin/bash
OUTPUT_DIR=/tmp
ant upgrade.system

EXEC_DATE=`date +%Y:%m:%d:%M`
for file in `ls -d $OUTPUT_DIR/Info*.csv` ; do
   ant upgrade.file -Dfile $file | tee -a $file.$EXEC_DATE.log &
done

and I'd like to run this as

bash test.sh | tee test.out

In my simple test this works, with all output going to test.out and background thread specific output going to $file.$EXEC_DATE.log.

Is this ok, or is tee'ing output in background threads dangerous for any reason?

Other people using this script have complained that output to test.out stops in some error conditions, even though background threads continue running.

Thanks, Jim

I think you mean processes. Threads are something entirely different, and something BASH can't manage.

"for file in `ls `" is a useless use of backticks. You could just do "for file in $OUTPUT_DIR/Info.csv" for the same effect without using ls or backticks at all.

On further checking, I see you are writing a different file for every process. This is safe. So is putting it behind another tee. Why is it dying? Make sure you're not exceeding the users' limit for the maximum number of processes they're allowed.

1 Like