Hari Sekhon wrote: > I have written a script and I would like to ensure that the script is > never run more than once at any given time. > > What is the best way of testing and exiting if there is another version > of this script running somewhere on this machine? > > I guess what I'm asking is how to handle system processes the way I can > in shell. I am running this on linux.
Although the other solution suggested seems to be more general (I have not read the code, only the info in the header), the following may be sufficient for what you want, and can be used as code inside of your script. It searches /proc/ for another process that is a script interpreted by python and has the same name as your script. So it can fail, if there is another python script running with the same name, but which is not identical to your script. OTOH it's a very short and simple test: ----- #!/bin/env python import os.path import linecache pid=os.getpid() script=linecache.getline(os.path.join('/proc', str(pid), 'cmdline'), 1).split('\0')[0:2] for pid in filter(lambda x: x.isdigit() and x != str(pid), os.listdir('/proc')): other=linecache.getline(os.path.join('/proc', str(pid), 'cmdline'), 1).split('\0')[0:2] if script[0] == other[0] and os.path.basename(script[-1]) == os.path.basename(other[-1]): raise "already running!" ----- HTH Stephan -- http://mail.python.org/mailman/listinfo/python-list