Have each subshell write stderr and stdout to its own logfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Have each subshell write stderr and stdout to its own logfile
# 1  
Old 09-10-2014
Have each subshell write stderr and stdout to its own logfile

Hello,

As stated in the title, I do some hacked parallel processing by running multiple instances of bash scripts, each in their own subshell. The code looks like this,
Code:
# launch one batch-train script in background for each value in fold group list
for FOLD_GROUP in "${FOLD_GROUP_LIST[@]}"
do
   # continue training on $FOLD_GROUP folds
   ./01_batch_script.sh $CORES \
                        $BATCH_PROJ \
                        $BATCH_STOP_ON_SUBSET \
                        $BATCH_STOP_ON_STAT \
                        $SET \
                        $FOLD_GROUP \
                        $RND_SEED \
                        $DATASET_STRING \
                        $ERRTOL \
                        $BATCHES \
                        $START_MODE \
                        $START_LR \
                        $MAX_EPOCH_BATCH \
                        $OA_PRINT_PRECISION \
                        $BATCH_PROC &
   # to prevent terminal overrun
   sleep 4
done

# wait for all subshells to return before resuming
wait

Despite the use of sleep to try and space things out, when I am working on these scripts I am writing allot of output and there is no way to keep the output from getting muddled when it is all going to one terminal.

I can generally work on just one instance if I need to, but it would also be nice to have each script report to its own log file so I can see which instance errors are associated with.

Is there a reasonable way to do this that someone can suggest?

LMHmedchem
# 2  
Old 09-10-2014
Before your loop add:
Code:
bkjobnum=1

In your loop, change:
Code:
                        $BATCH_PROC &
   # to prevent terminal overrun
   sleep 4

to:
Code:
                        $BATCH_PROC > 01_batch_script.out.$bkjobnum 2> 01_batch_script.err.$bkjobnum &
   # to prevent terminal overrun
   bkjobnum=$((bkjobnum + 1))

Then you will have the standard output and standard error output from each background job in separate files that you can peruse at your leisure.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-10-2014
Thanks, that will be a big help.

Is,

01_batch_script.out.$bkjobnum

the name of the file where stdout will be logged?


Would I be able to so something comparable to,

2>&1 | tee -a logfile.txt

to allow output to both the terminal and a logfile?

LMHmedchem
# 4  
Old 09-10-2014
Quote:
Originally Posted by LMHmedchem
Thanks, that will be a big help.

Is,

01_batch_script.out.$bkjobnum

the name of the file where stdout will be logged?


Would I be able to so something comparable to,

2>&1 | tee -a logfile.txt

to allow output to both the terminal and a logfile?

LMHmedchem
01_batch_script.out.$bkjobnum is the name of the file where stdout will be logged and 01_batch_script.err.$bkjobnum is the name of the file where stderr will be logged for each background job. And $bkjobnum will be incremented for each background job.

If you use tee -a logfile.txt the output in logfile.txt and on the screen will be muddled as before.
# 5  
Old 09-10-2014
It's sometimes handy to directly use the loop variable instead of a counter variable
Code:
./01_batch_script.sh ... > $FOLD_GROUP.out 2>&1 &

This one redirects both stderr and stdout.
I recommend to have at least a sleep 1, to avoid load peaks (e.g. out-of-sockets).
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 09-11-2014
Quote:
Originally Posted by MadeInGermany
Code:
./01_batch_script.sh ... > $FOLD_GROUP.out 2>&1 &

I ended up using this version because it created fewer log files and I took this advice to use some additional data from my script to make up the log file name.

It would be nice to have some output to the terminal to follow progress, but it is easy enough to check the log files. The text editor that I use will reload the log file if there is a change.

This is what it looks like now,
Code:
# launch one batch-train script in background for each value in fold group list
for FOLD_GROUP in "${FOLD_GROUP_LIST[@]}"
do
   # continue training on $FOLD_GROUP folds
   ./01_batch_script.sh $CORES \
                        $BATCH_PROJ \
                        $BATCH_STOP_ON_SUBSET \
                        $BATCH_STOP_ON_STAT \
                        $SET \
                        $FOLD_GROUP \
                        $RND_SEED \
                        $DATASET_STRING \
                        $ERRTOL \
                        $BATCHES \
                        $START_MODE \
                        $START_LR \
                        $MAX_EPOCH_BATCH \
                        $OA_PRINT_PRECISION \
                        $BATCH_PROC > 'logfile_'$SET'_fg'$FOLD_GROUP'_'$START_MODE'-'$START_LR'.txt' 2>&1 &
   # to prevent terminal overrun
   sleep 2
done

# wait for all subshells to return before resuming
wait

LMHmedchem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?

(Apologies for any typos.) OSX 10.12.3 AND Windows 10. This is for the serious Python experts on at least 3.5.x and above... In script format sys.stdout.write() AND sys.stderr.write() seems to work correctly. Have I found a serious bug in the interactive sys.stdout.write() AND... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Solaris

SMF in Solaris wont write to stderr/stdout ?

Hi, I got a process (c written) that we usually run this way : EmsChkQu >> /EMS/log/EmsChkQu.log 2>&1 When trying to use it as a service I defined it this way : <?xml version='1.0'?> <!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'> <service_bundle... (6 Replies)
Discussion started by: zionassedo
6 Replies

3. Shell Programming and Scripting

stdout, stderr redirection

Hi all, can someone help me with the next redirection? i want to redirect the stdout+stderr of a command to the same file (this i can do by prog &> file) but in addition i want to redirect only the stderr to a different file. how can i do this please? (in BASH) thanks. (4 Replies)
Discussion started by: eee
4 Replies

4. Shell Programming and Scripting

stderr/stdout

Can somebody explain to me why the diff output is not going to stderr? Yet when I issue a diff from the command line the return code is -ne 1. I am guessing diff always writes to stdout??? Is there away I can force the difff to write to stderr USING THE CURRENT template. If possible, I... (5 Replies)
Discussion started by: BeefStu
5 Replies

5. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

6. Shell Programming and Scripting

redirect STDOUT to a file in a subshell

Hi, I would like to avoid re-directing line by line to a file. What is the best way to re-direct STDOUT to a file in a subshell? Thanks in advance. Cheers Vj (1 Reply)
Discussion started by: tnvee
1 Replies

7. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

8. Shell Programming and Scripting

precedence of stderr and stdout

#!/usr/bin/perl open(STDOUT, ">>$Textfile") open(STDERR, ">>$Textfile") print "program running\n"; $final = join("+", $initial,$final) #5 close (STDOUT); close (STDERR);Hi all, above is my perl code. Notice i have captured the stdout and stderr to the same textfile. my code is expected to... (1 Reply)
Discussion started by: new2ss
1 Replies

9. Shell Programming and Scripting

Start subshell with different STDOUT

Hello Before I get to my question let me explain the situation. I am writing a ksh script to startup several instances of an application. That is done by executing another ksh script (let's call it startApp) with some arguments. Now, startApp writes some information to stdout that I don't want... (3 Replies)
Discussion started by: EvilBoz
3 Replies

10. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies
Login or Register to Ask a Question