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:
#!/usr/bin/env bash
PID=$$
echo "PID is $PID"
echo $PID > my_script.pid
echo "Sleeping..."
sleep 5
echo "Finished"
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:(
Please, help!
Update. My goal is to define if another instance of that script is still running. I decided to use pid file and condition:
PID=`cat $PID_FILE`
if ps -p $PID > /dev/null; then
echo "script already running"
exit 1
fi
Maybe there is a more efficient way?