[Tutor] checking for a process

John Moylan john at rte.ie
Wed Aug 27 17:18:13 EDT 2003


This looked like a usefull but really dirty hack at first, on on later
inspection - it will probably have the effect of not allowing the script
to run if the same file is open in an editor or in less somewhere else
on the system.

so I wrote the following class to check /proc to see if a process is
running:

class IsPidRunning:
    def __init__(self,pidfile="/var/run/enc.pid"):
        self.pidfile = pidfile

    def set_pidfile(self,pidfile):
        self.pidfile = pidfile

    def doesitexist(self):
        if  os.path.exists(self.pidfile):
            pidf = open(self.pidfile, 'r')

            if os.path.exists('/proc/'+pidf.read()):
                print "Script already running"
                sys.exit()
            else: self.writepid()
        else: self.writepid()

    def writepid(self):
        pidf = open(self.pidfile, 'w')
        pid = os.getpid()
        print pid
        pidf.write(str(pid))
        pidf.close()


John


On Wed, 2003-08-27 at 09:55, David Dorgan wrote:
> Quoting John Moylan (john at rte.ie):
> > The problem is that I don't want to have to create a BASH wrapper script 
> > around my python script ....but I can't find any way of finding out if a 
> > process is running ala the ps bit of my script above...without spawning a 
> > shell. Is there any way to do this natively in Python? or is there a better 
> > way of creating the same functionality in Python? 
> 
> 
> If you want a real awful hack, but probably
> a little better than what you have, use popen2,
> run ps aux | grep -v grep | grep -c whatever  
> 
> if the return is greater than 0, it's running,
> else, run it.
-- 
John Moylan
----------------------
Radio Telefis Eireann,
Donnybrook,
Dublin 4,
Eire
t:+353 1 2083564
e:john.moylan at rte.ie



******************************************************************************
The information in this e-mail is confidential and may be legally privileged.
It is intended solely for the addressee.  Access to this e-mail by anyone else
is unauthorised.  If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in reliance
on it, is prohibited and may be unlawful.
Please note that emails to, from and within RTÉ may be subject to the Freedom
of Information Act 1997 and may be liable to disclosure.
******************************************************************************



More information about the Tutor mailing list