title: Ruby background process STDOUT is empty
body: <p>I'm having a weird issue with a start-up script which runs a Sinatra script using the shell's "daemon" function. The problem is that when I run the command at the command line, I get output to STDOUT. If I run the command at the command line exactly as it is in the script -- less the daemon part -- the output is correctly redirected to the output file. However, when the startup script runs it (see below), I get stuff to the STDERR log but not to the STDOUT log.</p>
  
  <p>The relevant lines of the script:</p>
  
  <pre><code>#!/bin/sh
  # (which is and has been a symlink to /bin/bash
  
  # Source function library.
  . /etc/init.d/functions
  
  # Set Some Variables
  RUNAS="joeuser"
  PID=/var/run/myapp.pid
  LOG="/var/log/myapp/app-out.log"
  ERR_LOG="/var/log/myapp/app-err.log"
  APPLICATION_COMMAND="RAILS_ENV=production ruby /opt/myapp/lib/daemons/my-sinatra-app.rb -p 8002 2&gt;&gt;${ERR_LOG} &gt;&gt;${LOG} &amp;"
  
  # Snip a bunch. This is the applicable line from the "start" case:
  daemon --user $RUNAS --pidfile $PID $APPLICATION_COMMAND &amp;&gt; /dev/null
  </code></pre>
  
  <p>Now, the funky parts:</p>
  
  <ul>
  <li>The error log is written to correctly via the redirect of STDERR.</li>
  <li>If I reverse the order of the >> and the 2>> (I'm grasping at straws, here!), the behavior does not change: I still get STDERR logged correctly and STDOUT is empty.</li>
  <li>If the output log doesn't exist, the STDOUT redirect creates the file. But, the file remains 0-length.</li>
  <li>This used to work. The log directory is maintained by log-rotate. All of the more-recent 'out' logs are 0-length. The older ones are not. It seems like it stopped working some time in April. The ruby code didn't change at any time near then; neither did the startup script.</li>
  </ul>
  
  <p>We're running three different services in this way. Two of them are ruby daemons (one uses sinatra, one does not) and the other is a background java process. This is occurring for BOTH of the ruby processes but is not happening on the java process. Maybe something changed in Ruby?</p>
  
  <p>FTR, we've got ruby 1.8.5 and RHEL 5.4.</p>
  
  <hr>
  
  <p>I've done some more probing. The <code>daemon</code> function does a bunch of stuff, but the meat of the matter is that it runs the program using <code>runuser</code>. The command essentially looks like this:</p>
  
  <pre><code>runuser -s /bin/bash - joeuser -c "ulimit -S -c 0 &gt;/dev/null 2&gt;&amp;1 ; RAILS_ENV=production ruby /opt/myapp/lib/daemons/my-sinatra-app.rb -p 8002 '&lt;/dev/null' '&gt;&gt;/var/log/myapp/app-out.log' '2&gt;&gt;/var/log/myapp/app-err.log' '&amp;'"
  </code></pre>
  
  <p>When I run exactly that at the command line (both with and without the single-ticks that got added somewhere along the line), I get the exact same screwy behavior w.r.t. the output log. So, it seems to me that this is an issue of how ruby (?) interacts with <code>runuser</code>?</p>