Detecting if a program is currently running.

Discussion in 'Python' started by Brian, Dec 27, 2004.

  1. Brian

    Brian Guest

    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?
     
    Brian, Dec 27, 2004
    #1
    1. Advertising

  2. Brian

    Mike Meyer Guest

    Brian <> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 27, 2004
    #2
    1. Advertising

  3. Brian

    Brian Guest

    Thanks, that does the trick.


    Mike Meyer <> wrote in news::

    > 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
     
    Brian, Dec 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guy
    Replies:
    1
    Views:
    718
    Jonck
    Sep 27, 2004
  2. Peter Blatt
    Replies:
    0
    Views:
    401
    Peter Blatt
    Oct 9, 2004
  3. shraddha
    Replies:
    10
    Views:
    6,182
    shraddha
    May 31, 2005
  4. Replies:
    7
    Views:
    393
  5. Yeounkun, Oh

    How can I get a name of function running currently.

    Yeounkun, Oh, Nov 16, 2004, in forum: C Programming
    Replies:
    2
    Views:
    467
    Stuart Gerchick
    Nov 16, 2004
Loading...

Share This Page