multiple instance on Unix

Jorgen Grahn jgrahn-nntq at
Wed Sep 29 23:59:30 CEST 2004

On Wed, 29 Sep 2004 18:38:06 +0100, Nigel King <nigel.king at> wrote:
> Hi,
> I have (my son has!) implemented protection against multiple instances 
> causing havoc by creating a directory. This fails if it exists and thus 
> in a single instruction one gets both the acquire and the test.

I assume you want a program (when started) to exit immediately if another
instance is running, as the same user, on the machine?

> Windows has it's mutex which solves the problem. Is there any better 
> version for UNIX.

Ok, so Windows has named mutexes which work across processes.

Unix programs normally don't attempt to "protect" themselves in this way. I
can see no harm in two instances of a program running. I can /definitely/
see situations where two programs modifying the same set of files, resulting
in binary garbage, but if these are different programs your approach doesn't

Best is a design which doesn't need locking. Second best is locking which is
tied to the resources (files) which need to be protected. Leaving it up to
the user to be careful is another popular choice ;-)

For reference, have a look at procmail's lockfile(1) and mutt's
mutt_dotlock(1). Both deal with protecting mailbox files.


  // Jorgen Grahn <jgrahn@       Ph'nglui mglw'nafh Cthulhu
\X/      >   R'lyeh wgah'nagl fhtagn!

More information about the Python-list mailing list