title: How to write info to the file from bash script and see the result immediately?
body: <p>I need help. I can't understand how I can write information to the file from bash script and see the result immediately.
  For example:</p>
  
  <pre><code>#!/usr/bin/env bash
  PID=$$
  echo "PID is $PID"
  echo $PID &gt; my_script.pid
  echo "Sleeping..."
  sleep 5
  echo "Finished"
  </code></pre>
  
  <p>PID number appears in console immediately, but in the file I see it after script finished.
  I have Mac OS X Yosemite 10.10.3.
  I tried a lot of stuff with flush buffering. NO result:(</p>
  
  <p>Please, help!</p>
  
  <p>Update.
  My goal is to define if another instance of that script is still running. I decided to use pid file and condition:</p>
  
  <pre><code>PID=`cat $PID_FILE`
  if ps -p $PID &gt; /dev/null; then
       echo "script already running"
       exit 1
  fi
  </code></pre>
  
  <p>Maybe there is a more efficient way?</p>