Making sure script only runs once instance at a time.

Hari Sekhon hpsekhon at googlemail.com
Fri Sep 29 09:54:43 EDT 2006


I'm not sure if that is a very old way of doing it, which is why I was 
reluctant to do it. My way actually uses the process list of the os 
(linux) and counts the number of instances. If it is more than 0 then 
another process is running and the script exits gracefully.

Also, apart from the fact the using lockfiles feels a bit 1970s, I have 
found that in real usage of other programs within the company that use 
lockfiles, it sometimes causes a bit of troubleshooting time when it 
stops working due to a stale lockfile. This especially happens when the 
program is killed, the lockfile remains and causes minor annoyance (to 
somebody who knows that is, more annoyance to somebody who doesn't).

-h

Hari Sekhon



Paul Rubin wrote:
> Hari Sekhon <hpsekhon at googlemail.com> writes:
>   
>> Seeing as there doesn't seem to be a good answer to this (or at least
>> not one that we have so far some up with) I have decided to fall back
>> to my old friend the unix shell. It's as portable as python, but is
>> very flexible and fast at doing real things and will tell me if
>> another process by this name is running. If so, print msg and
>> exit. simple.
>>     
>
> Huh?  The obvious way to check for another instance is with a lock
> file.  Just open the file and use fcntl to set an exclusive lock.  If
> the lock acquisition fails, another instance has the file.
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060929/906d3cea/attachment.html>


More information about the Python-list mailing list