title: Reference for proper handling of PID file on Unix
body: <p>Where can I find a <strong>well-respected reference</strong> that details the proper handling of PID files on Unix?</p>
  
  <p>On Unix operating systems, it is common practice to “lock” a program (often a daemon) by use of a special lock file: the PID file.</p>
  
  <p>This is a file in a predictable location, often ‘/var/run/foo.pid’. The program is supposed to check when it starts up whether the PID file exists and, if the file does exist, exit with an error. So it's a kind of advisory, collaborative locking mechanism.</p>
  
  <p>The file contains a single line of text, being the numeric process ID (hence the name “PID file”) of the process that currently holds the lock; this allows an easy way to automate sending a signal to the process that holds the lock.</p>
  
  <p>What I can't find is a good reference on expected or “best practice” behaviour for handling PID files. There are various nuances: how to actually lock the file (don't bother? use the kernel? what about platform incompatibilities?), handling stale locks (silently delete them? when to check?), when exactly to acquire and release the lock, and so forth.</p>
  
  <p>Where can I find a <strong>respected, most-authoritative reference</strong> (ideally on the level of W. Richard Stevens) for this small topic?</p>