Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Detecting if a program is currently running.

Reply
Thread Tools

Detecting if a program is currently running.

 
 
Brian
Guest
Posts: n/a
 
      12-27-2004
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?
 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      12-27-2004
Brian <(E-Mail Removed)> 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      12-27-2004
Thanks, that does the trick.


Mike Meyer <(E-Mail Removed)> wrote in news:(E-Mail Removed):

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: How to find a file or a device is currently used by whichprocessor which program ? Tim Golden Python 0 10-11-2006 09:27 AM
How to find a file or a device is currently used by which processor which program ? fdu.xiaojf@gmail.com Python 0 10-10-2006 08:17 AM
Detecting a PIPE in a program Stu C Programming 9 02-04-2005 12:56 PM
Detecting the currently connected user to Tomcat Guy Java 1 09-27-2004 10:12 AM
detecting if program is running under X windows or not Paul Faulstich Perl 1 01-10-2004 07:16 PM



Advertisments