Search code examples
bashsshteeprocess-substitutionrexec

When tee command redirect to subshell, the last two lines missing


I have below solution to record a command and its output executed on a remote machine:

rexec:// -t -t /usr/bin/ssh -q -x -o StrictHostKeyChecking=no -2 \
         -l ${SSHUserName} -p 22 ${mainHost} \
 | tee >(/opt/oss/clilogging/bin/clilogging.sh para1 para2)

clilogging.sh will record each command and its output into a log file.
However, sometimes the last exited command and its output message "logout" is not written into the log file.

clilogging.sh is as follows:

#!/bin/bash

{
    while read R || [ -n "$R" ];do
        #e.g. 2013-08-19T09:58:08+0300
        timestamp=`date +%FT%T%z`;
        echo $timestamp $R;
    done
} > /tmp/xxx.log

Could anybody help me? Thanks a lot!


Solution

  • Thanks thom's comment and thank you all.
    I have found the solution of this issue.

    Need add following code at the begining of clilogging.sh

    trap "" HUP
    

    The meaning of code is to handle SIGHUP signal, here I ignore this signal, then clilogging.sh
    will not quit immediately and have the chance to handle all buffer.

    man 7 signal

       Signal     Value     Action   Comment
       -------------------------------------------------------------------------
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process