471,119 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

Detecting if a program is currently running.

On a Slackware 9.1 box, I'm trying to detect if mpg123 is currently
running/playing a song so that when the song is done, it'll play the next
in the list. The problem is that popen'ing ps doesn't always give a
correct return. My function is below and I added a second check that runs
1 second later which sometimes works, sometimes doesn't.

Here's my function:

def pid_defunct():
#1 = defunct processes
#2 = show is currently playing
#0 = no mpg123 processes found
t = os.popen( "ps axc | grep '[mpg123] <defunct>'" ).read()
if len(t) > 0:
return 1
else:
t = os.popen( "ps axc | grep 'mpg123'" ).read()
if len(t) > 0:
return 2
else:
print "Check 1: mpg123 is dead"
#Check twice, because popen doesn't always return
#correct value
time.sleep(1)
t = os.popen( "ps axc | grep 'mpg123'" ).read()
if len(t) > 0:
print "Check 1 was wrong"
return 2
else:
print "Check 2: mpg123 is dead"
return 0
This function is run during each iteration of a loop with a time.sleep(1)
executed right before it. Is there a better way to make this work?
Jul 18 '05 #1
2 3102
Brian <Th*************@ddress.com> writes:
On a Slackware 9.1 box, I'm trying to detect if mpg123 is currently
running/playing a song so that when the song is done, it'll play the next
in the list. The problem is that popen'ing ps doesn't always give a
correct return. My function is below and I added a second check that runs
1 second later which sometimes works, sometimes doesn't.

Here's my function:

def pid_defunct():
#1 = defunct processes
#2 = show is currently playing
#0 = no mpg123 processes found
t = os.popen( "ps axc | grep '[mpg123] <defunct>'" ).read()
if len(t) > 0:
return 1
else:
t = os.popen( "ps axc | grep 'mpg123'" ).read()
if len(t) > 0:
return 2
else:
print "Check 1: mpg123 is dead"
#Check twice, because popen doesn't always return
#correct value
time.sleep(1)
t = os.popen( "ps axc | grep 'mpg123'" ).read()
if len(t) > 0:
print "Check 1 was wrong"
return 2
else:
print "Check 2: mpg123 is dead"
return 0
This function is run during each iteration of a loop with a time.sleep(1)
executed right before it. Is there a better way to make this work?


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2
Thanks, that does the trick.
Mike Meyer <mw*@mired.org> wrote in news:86************@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


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Jay | last post: by
7 posts views Thread by fox | last post: by
7 posts views Thread by Aarti | last post: by
reply views Thread by David Eales | last post: by
1 post views Thread by wwwords | last post: by
5 posts views Thread by NeBlackCat (lists) | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.