Detecting if a program is currently running.

Brian ThisIsNotMyReal at ddress.com
Mon Dec 27 08:29:02 EST 2004


Thanks, that does the trick.


Mike Meyer <mwm at mired.org> wrote in news:864qi85ngf.fsf at guru.mired.org:

> Yeah, but it takes work on both ends. You could wrap your mpg123 in a
> shell script like so:
> 
> #!/bin/sh
> mpg123 "$@" &
> echo $! >/tmp/mpg123.pid
> 
> 
> Or in python 2.4:
> 
> #!/usr/bin/env python
> from subprocess import Popen
> 
> p = Popen('mpg123')
> pidfile = file('/tmp/mpg123.pid', 'w')
> pidfile.write("%d" % p.pid)
> pidfile.close()
> 
> Then have your check program do (again, using the 2.4 subprocess
> module) 
> 
> from subprocess import Popen, PIPE
> 
> try:
>      pidfile = file('/tmp/mpg123.pid')
> except IOError:
>      print 'mpg123 is dead'
> else:
>      pid = pidfile.read()
>      t = Popen('ps p %s' % pid, shell=True, stdout=PIPE).stdout
>      if t.readlines() < 2:
>          print 'mpg123 is dead'
>          os.remove('/tmp/mpg123.pid')
>          return 0
>      return 2
> 
> Basically, instead of trusting grep to find mpg123, save the pid in a
> file and let ps find it (or not) by pid.
> 
>      <mike




More information about the Python-list mailing list