I have a bash script at work called the batch launcher. It is responsible for launching and reporting on the status of various batches. The launch of the batches is done like this:
env $BENV setsid $BATCH $OPT >> $FILE 2>&1
Several months ago I encountered an issue where a batch with forked children sent a kill signal on PGID, which ended up killing the batch AND the batch launcher, which is a big no-no. My solution to the problem was to add the setsid
part (since bash does not allow setpgrp
), which creates a new session and as a result - a new PGID.
Now I have another issue. That same pesky batch with forked children, except the children are not writing any logs until their job completion. After investigating the issue, I found the reason here - setsid
spawns a process with no TTY, in which case any output from the children does not get autoflushed on every newline, but rather gets flushed once at the end right before stream closure. This means that if a child dies with an error, the logs in the buffer will be gone forever proving any debugging impossible.
Is there a way to tell setsid
to autoflush STDOUT/STDERR output streams? Googling this has yielded me no results, and my only solution at this point is to rewrite the batch launcher in a language that supports setpgrp
like PERL or C, which will require significant testing from QA.
EDIT:
Minor note, the issue can be resolved with:
env $BENV script -qec "$BATCH $OPT" -af $FILE
This requires util-linux
older than 2010 to support -e
flag for return code processing (which unfortunately is not the case for me with RHEL5 from 2005, see commit for details).