avoid script running twice

Tim Williams tim at tdw.net
Mon Jun 18 15:07:06 EDT 2007


On 18/06/07, Robin Becker <robin at reportlab.com> wrote:
> Wildemar Wildenburger wrote:
> > Robin Becker wrote:
> .........
> >
> > Well I can think of a dumb way: create a temporary file during the
> > transaction and have your script check for that before running its main
> > body.
> >
> >
> > I think thats the most hassle free way of doing it.
> > /W
> I looked at the temporary files idea, but I'm not certain about the exact
> details. Normally your create a file and then remove it whilst keeping the file
> handle; that allows your program to write to it whilst guaranteeing that it will
> vanish when you die, but this has to be a named file so that the second instance
> can check for it.


Doesn't that prevent it from being already removed? My unix
> experience is long, but fairly primitive.


You don't need to remove the file, write anything to it,  or check for
prior existance :)

If one process has the file open for writing, no other process can
open it for writing at the same time - and this is what you are
checking for!   The exception that the second concurrent open/write
attempt raises tells you that another instance of the script is
running.

This has the added advantage that should something go wrong, such as
an abnormal termination or reboot,  the open/write disappears and the
next time the script runs everything is back to normal.

HTH :)



More information about the Python-list mailing list