bash tee and background threads


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash tee and background threads
# 1  
Old 08-25-2010
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.

Code:
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

Last edited by Scott; 08-25-2010 at 01:14 PM.. Reason: Please use code tags
# 2  
Old 08-25-2010
Quote:
Originally Posted by heatlamp
Running centos 2.6, I have a bash script in which I'd like to run a number of background threads in parallel
I think you mean processes. Threads are something entirely different, and something BASH can't manage.
Quote:
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
"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.

Last edited by Corona688; 08-25-2010 at 01:22 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies

2. Shell Programming and Scripting

[Help] Bash script that runs in the background and checks for mails...

Hello! I have got a homework. The bash script runs in the background and checks the user's mailbox and when the user gets a new mail a popup window appears with some text and information about the sender (from who and when).I have no idea how to start, any help would be appreciated! Thank you:) (1 Reply)
Discussion started by: capo2ndfret
1 Replies

3. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

4. Shell Programming and Scripting

Suppress a background message in Bash

I'm having trouble with part of this bash script in Linux where I respawn a new instance of script and kill the old one to prevent forking (Yes, I know 'exec' will not fork but this needs to be interactive) When the old instance is kill it pops up "Terminated!" in the middle of the new instance... (7 Replies)
Discussion started by: Azrael
7 Replies

5. Shell Programming and Scripting

BASH - Handling background processes - distributed processing

NOTE: I am using BASH and Solaris 10 for this. Currently in the process of building a script that has a main "watcher" daemon that reads a configuration file and starts background processes based on it's global configuration. It is basically an infinite loop of configuration reading. Some of the... (4 Replies)
Discussion started by: dcarrion87
4 Replies

6. Shell Programming and Scripting

Background tasks in a loop (bash)

I am trying to use a loop to start tasks 0-3, running 0,1,2 in the background with &. FOLDSET=( 0 1 2 3 ) for FOLDSET in ${FOLDSET} do if ; then BACKGRD="&" else BACKGRD="" fi # start task $FOLDSET task1 -nogui -ni -p $PROJ \ epochs=$EPOS ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

7. Shell Programming and Scripting

background processing in BASH

I have script 3 scripts 1 parent (p1) and 2 children child1 and child2 I have script 3 scripts 1 parent 2 children child1 child2 In the code below the 2 child processes fire almost Instantaneously in the background, Is that possible to know the status of pass/fail of each process... (12 Replies)
Discussion started by: jville
12 Replies

8. Linux

background processing in BASH

I have script 3 scripts 1 parent 2 children child1 child2 In the code below the 2 child processes fire almost Instantaneously in the background, Is that possible to know the status of pass/fail of each process "as it happens" ? In the present scenario although Child2... (5 Replies)
Discussion started by: jville
5 Replies

9. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

10. Solaris

Best practice to run bash script in background

nohup /bin/bassh $HOME/scripts/test.sh > $HOME/log/test.log 2>&1 & nohup $HOME/scripts/test.sh > $HOME/log/test.log 2>&1 & Which is the good practice to run a script in background of above two ? does the first one will have any overhead on the system ? our system is SunOS 5.10... (2 Replies)
Discussion started by: mmasals
2 Replies
Login or Register to Ask a Question