PID lockfile

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Mar 26 05:04:47 EDT 2009


Ben Finney <ben+python at benfinney.id.au> writes:

> (replying in ‘comp.lang.python’ for wider feedback on this issue)
> 
> On 26-Mar-2009, Francis Irving wrote:
> > ourlockfile.path = '/tmp/mydaemon.pid'
> > context = daemon.DaemonContext(
> >     pidfile=ourlockfile,
> >     stdout=logout,
> >     stderr=logout
> > )
> > 
> > If so, it doesn't work, it just exits without an error.
                                          ^^^^^^^

> Can you please provide the full traceback?

My apologies; no of course you can't :-) Okay, I will have a look at
what might be happening; can I ask you to do some diagnosis as well to
see if you *can* get an informative output?

> > So, I think that PIDLockFile will leave the lockfile there if the
> > daemon is killed abruptly (say with "kill -9" or due to some bug).
> > Certainly, all the lock classes in lockfile.py have the problem
> > that it stays locked int hat circumstance.
> 
> I have my programs delete the lockfile on start-up if it's stale
> (i.e. check for the existence of the file on start-up and delete it
> if the referenced PID is not running). Perhaps there are better
> ways.

In the case of the ‘lockfile’ library, Skip is aiming for a
cross-platform solution, with atomic behaviour; he has implemented
lock acquisition with a ‘link’ operation on Unix, and a ‘mkdir’
operation on Windows.

But both of those, of course, create a new file. What I want is to
lock an existing file. Is ‘lockf’ particularly prone to cross-platform
troubles on Unix variants? (Since the “become a daemon” pattern
makes no sense on anything but Unix, I'm only concerned with PID file
behaviour that works on Unix.)

> > This page (which seems pretty good anyway, and I'm sure you've
> > seen!) section 6) suggests using lockf, although I believe from
> > elsewhere that fcntl will do also.
> > 
> > http://www.enderunix.org/docs/eng/daemon.php
> 
> No, I've not seen that, but I have seen others; they tend to differ
> in the details. I have looked for a more canonical reference for the
> intricacies of PID file handling, but it seems to be much more ad
> hoc than the definition of the daemonisation procedure.

I would very much like to see something akin to the canonical Stevens
references on other Unix topics, but specifically for “good PID file
management”. Does anyone have any pointers?

-- 
 \       “Faith, n. Belief without evidence in what is told by one who |
  `\   speaks without knowledge, of things without parallel.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney



More information about the Python-list mailing list