Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By alf55
I am using bash trying to log both stdout and stderr (interleaved as if they were appearing on the screen) to a log file, but I've encountered what look like ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    2

    Question Redirecting stderr and stdout does not work quite right


    I am using bash trying to log both stdout and stderr (interleaved as if they were appearing on the screen) to a log file, but I've encountered what look like bugs along the way.

    Here is what I want to see appended to the log file:

    Code:
    bl-swr7-sbc17:~ # rpm -U syslog-ng-3.3.5-7.9.1.gb12.x86_64.rpm           
    warning: /etc/syslog-ng/syslog-ng.conf saved as /etc/syslog-ng/syslog-ng.conf.rpmsave
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...
    redirecting to systemctl
    Note the "warning" line. It is absolutely critical to me, but it keeps disappearing.

    First try (I am omitting some extraneous commands):

    Code:
    bl-swr7-sbc17:~ # echo --BEGIN LOG-- > temp
    bl-swr7-sbc17:~ # rpm -U syslog-ng-3.3.5-7.9.1.gb12.x86_64.rpm >> temp 2>&1
    bl-swr7-sbc17:~ # cat temp
    redirecting to systemctl
    Note that the file is not appended to, but is truncated, and only the last line shows up.

    Next try, using a bash 4 feature:

    Code:
    bl-swr7-sbc17:~ # echo --BEGIN LOG-- > temp
    bl-swr7-sbc17:~ # rpm -U syslog-ng-3.3.5-7.9.1.gb12.x86_64.rpm &>> temp 
    bl-swr7-sbc17:~ # cat temp
    redirecting to systemctl
    Same result. Now it gets even weirder. Lest you think I can solve this manually ...

    Code:
    bl-swr7-sbc17:~ # rpm -U syslog-ng-3.3.5-7.9.1.gb12.x86_64.rpm 2> temp 
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...
    bl-swr7-sbc17:~ # cat temp
    redirecting to systemctl
    The "warning" line disappears.

    To round out the testing:
    Code:
    bl-swr7-sbc17:~ # rpm -U syslog-ng-3.3.5-7.9.1.gb12.x86_64.rpm > temp 
    warning: /etc/syslog-ng/syslog-ng.conf saved as /etc/syslog-ng/syslog-ng.conf.rpmsave
    redirecting to systemctl
    bl-swr7-sbc17:~ # cat temp
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...
    Updating /etc/sysconfig/syslog...

    1) Why do the "append" bash redirection operations not properly append? 2) Why does this one line keep going away?

    I've done these operations repeatedly to make sure I'm not omitting something. I'm using GNU bash, version 4.2.10(1)-release (x86_64-suse-linux-gnu) on OpenSuSE 12.1.

    Thanks for the help.
    Eric

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    668
    Here is how to do it.
    Code:
    function output
    {
        echo " 1) line 1 stdout"
        echo " 2) line 1 stderr" 1>&2
        echo " 3) line 2 stdout"
        echo " 4) line 3 stdout"
        echo " 5) line 2 stderr" 1>&2
        echo " 6) line 3 stderr" 1>&2
        echo " 7) line 4 stderr" 1>&2
        echo " 8) line 4 stdout"
    }
    
    # only see stdout
    output 2>/dev/null
    
    # only see stderr
    output >/dev/null
    
    echo "header line into stdout" > /tmp/tmp
    # combine stderr and stdout then append into a file
    output 2>&1 | cat >> /tmp/tmp
    results of "cat /tmp/tmp" is:
    Code:
    bash$    cat /tmp/tmp
    header line into stdout
     1) line 1 stdout
     2) line 1 stderr
     3) line 2 stdout
     4) line 3 stdout
     5) line 2 stderr
     6) line 3 stderr
     7) line 4 stderr
     8) line 4 stdout
    >> temp ------ re-directs the stuff in stdout to append into the file temp (the application still knows the original location for stdout)
    2>&1 ---- put the stderr into the location of the original stdout
    drl likes this.

  3. #3
    Just Joined!
    Join Date
    May 2011
    Posts
    2
    That worked, thanks!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •