title: Truncating a file while it's being used (Linux)
body: <p>I have a process that's writing a lot of data to stdout, which I'm redirecting to a log file.  I'd like to limit the size of the file by occasionally copying the current file to a new name and truncating it.</p>
  
  <p>My usual techniques of truncating a file, like</p>
  
  <pre><code>cp /dev/null file
  </code></pre>
  
  <p>don't work, presumably because the process is using it.</p>
  
  <p>Is there some way I can truncate the file?  Or delete it and somehow associate the process' stdout with a new file?</p>
  
  <p>FWIW, it's a third party product that I can't modify to change its logging model.</p>
  
  <p><strong>EDIT</strong> redirecting over the file seems to have the same issue as the copy above - the file returns to its previous size next time it's written to:</p>
  
  <pre><code>ls -l sample.log ; echo &gt; sample.log ; ls -l sample.log ; sleep 10 ; ls -l sample.log
  -rw-rw-r-- 1 user group 1291999 Jun 11  2009 sample.log
  -rw-rw-r-- 1 user group 1 Jun 11  2009 sample.log
  -rw-rw-r-- 1 user group 1292311 Jun 11  2009 sample.log
  </code></pre>