Making sure script only runs once instance at a time.

Hari Sekhon hpsekhon at googlemail.com
Mon Oct 2 13:28:41 EDT 2006


The name thing is taken care of by the fact that I use the path not just 
the script name. The path appears in the command and then is grepped out 
by that criteria, so if there was another program with the same name and 
in a different path then it would not affect it.

Of course this is defeatable now I think about it if both programs are 
called the same thing and both are run as ./program_name. I could use 
the args to test too but this is also defeatable if being pedantic, and 
I don't want to compound any errors, I'm sure I do that enough without 
trying...

So I guess we come back to using the pids in the lockfiles as you 
suggested and test against those.

I was thinking that I could just adjust it to find the pid of the 
program by location of the program on the filesystem and if there is a 
pid for the program then there is a running instance of this exact 
program in  that directory. This would bypass the lockfile thing again 
but I've just tested this and it doesn't work, the pid will be found for 
python but not for the python program so it breaks this - dang, that was 
a very nice thing to do with binaries, I guess it just won't work with 
python in the same way (or any other interpreted language I expect).

I like your method too, it's very pythonic. I'm still weening off bash a 
bit as you can tell...

I guess there is no escaping the lockfile at this stage unless I can 
think of something else...

I may rewrite safety to use your lockfile with pid embedded. I've 
actually used a very similar method in bash before for something, saving 
the pid and env vars and then using the binary kill -0 <pid> to test if 
a program is alive by finding out whether a signal could be sent to it. 
Works nicely there too.

-h

Hari Sekhon



Fredrik Lundh wrote:
> Hari Sekhon wrote:
>
>   
>> How exactly do you check that the pid is still active in python? Is 
>> there a library or something that will allow me to manipulate system 
>> processes and listings etc the way everybody does in unix shells....
>>     
>
> by passing zero to the os.kill primitive:
>
> 	os.kill(pid, 0)
>
> if this raises an OSError, there's no active process with the given pid.
>
>   
>> I'm a huge fan of shell so I've done my own thing which leans on shell 
>> as follows:
>>
>> import sys,commands,os
>>
>> scriptpath = sys.argv[0]
>> scriptname = os.path.basename(scriptpath)
>>
>> number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc 
>> -l' % scriptpath)
>>
>> if number_procs > 1:
>>     print "There appears to be another %s process running." % scriptname
>>     
>
> what if you have commands with overlapping names (e.g. "bar.py" and 
> "foobar.py"), or some other user on the machine happens to run a
> command that, on purpose or by accident, contains your script's name 
> (e.g. "emacs mybar.py") ?
>
> </F>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061002/db61d2fd/attachment.html>


More information about the Python-list mailing list