title: Self-logging: Busybox shell script that logs stdout to a file
body: <p>My question relates to an answer posted by jbarlow to the following question:
  <a href="https://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself">redirect COPY of stdout to log file from within bash script itself</a></p>
  
  <p>I used the suggested script as listed below.  I have to use this because I don't have access to full bash (as jbarlow points out) because I'm using a buildroot version of busybox.</p>
  
  <pre><code>#!/bin/sh
  
  if [ "$SELF_LOGGING" != "1" ]
  then
      PIPE=tmp.fifo
      mkfifo $PIPE
  
      # Keep PID of this process
      SELF_LOGGING=1 sh $0 $* &gt;$PIPE &amp;
      PID=$!
  
      tee logfile &lt;$PIPE &amp;
  
      # Safe to rm pipe because the child processes have references to it
      rm $PIPE    
      wait $PID
  
      # Return same error code as original process
      exit $?
  fi
  </code></pre>
  
  <p>The issue I'm finding is that it appears that something can freeze up from this script.  For example, an strace of a frozen script using the above code looks like:</p>
  
  <pre><code>Process 29750 attached - interrupt to quit
  open("/tmp/tmp.fifo", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
  write(2, "/usr/bin/runStuff", 24) = 24
  write(2, ": ", 2)                       = 2
  write(2, "line ", 5)                    = 5
  write(2, "45", 2)                       = 2
  write(2, ": ", 2)                       = 2
  write(2, "can't open ", 11)             = 11
  write(2, "/tmp/tmp.fifo", 21)   = 21
  write(2, ": ", 2)                       = 2
  write(2, "no such file", 12)            = 12
  write(2, "\n", 1)                       = 1
  stat64("/sbin/tee", 0xbff7c20c)         = -1 ENOENT (No such file or directory)
  stat64("/usr/sbin/tee", 0xbff7c20c)     = -1 ENOENT (No such file or directory)
  stat64("/bin/tee", 0xbff7c20c)          = -1 ENOENT (No such file or directory)
  stat64("/usr/bin/tee", {st_mode=S_IFREG|0755, st_size=18956, ...}) = 0
  _exit(1)                                = ?
  Process 29750 detached
  </code></pre>
  
  <p>What it looks like (to me, with limited knowledge in this) is that tee is ending and the parent script doesn't die.  Is that correct?  If so, shouldn't the lack of a readable file cause the script to end?  tee is backgrounded, so obviously that has no control over the parent.</p>
  
  <p>As background, there's another process that repeatedly calls this if it dies.  So it's possible that using the same file is causing a lockup situation.  Or maybe the rm is happening <em>before</em> the fifo is created?</p>
  
  <p>I've considered using 'read' with a timeout, but there can be situations where nothing is logged for hours at a time.</p>
  
  <p>Can the script be modified so that this doesn't lock up and the script will die if/when <em>one</em> of the ends of the fifo dies?</p>