<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
AMENDMENT:<br>
<br>
The line <br>
<br>
number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc
-l' % scriptpath)<br>
<br>
was supposed to be<br>
<br>
number_procs=int(commands.getstatusoutput('ps -ef|grep %s|grep -v
grep|wc
-l' % scriptpath)[1])<br>
<br>
-h<br>
<br>
<pre class="moz-signature" cols="72">Hari Sekhon</pre>
<br>
<br>
Hari Sekhon wrote:
<blockquote cite="mid4520E5E1.3040409@googlemail.com" type="cite">
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
Fredrik Lundh wrote:
  <blockquote cite="midefjbhh$d9m$1@sea.gmane.org" type="cite">
    <pre wrap="">Hari Sekhon wrote:

  </pre>
    <blockquote type="cite">
      <pre wrap="">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.
    </pre>
    </blockquote>
    <pre wrap=""><!---->
the code that reliably identifies instances of a given program would be 
interesting to see.

  </pre>
    <blockquote type="cite">
      <pre wrap="">Also, apart from the fact the using lockfiles feels a bit 1970s, I have
    </pre>
    </blockquote>
    <pre wrap=""><!----> > 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.

to minimize that risk, store the pid in the lockfile (and preferrably 
also the host name), and make sure that the program checks that the pid 
is still active before it "stops working".

</F>

  </pre>
  </blockquote>
  <br>
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....<br>
  <br>
I'm a huge fan of shell so I've done my own thing which leans on shell
as follows:<br>
  <br>
import sys,commands,os<br>
  <br>
scriptpath = sys.argv[0]<br>
scriptname = os.path.basename(scriptpath)<br>
  <br>
number_procs=commands.getstatusoutput('ps -ef|grep %s|grep -v grep|wc
-l' % scriptpath)<br>
  <br>
if number_procs > 1:<br>
    print "There appears to be another %s process running." % scriptname<br>
    print "Please do not run more than one instance of this program"<br>
    print "Quitting for safety..."<br>
    sys.exit(200)<br>
  <br>
This works nicely for me.<br>
  <br>
You might also want to add a bit of discrimination to the script
(something along the lines of "get a decent os"...since this won't work
on windows)<br>
  <br>
import platform<br>
if platform.system() != 'Linux':<br>
    print "Sorry but this program can only be run on Linux"<br>
    sys.exit(201)<br>
    #todo: should do a bit extra for our bsd cousins here....<br>
  <br>
  <br>
Let me know if you think you have a better way. Please provide a code
snippet if so.<br>
  <br>
-h<br>
</blockquote>
</body>
</html>