473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3233
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
7
by: Joecx | last post by:
Hi Does anyone have the code or maybe give me a start on how to detect if my program is already running if someone tries to run it again while it's already running? I know I could do this with a...
7
by: fox | last post by:
Maybe this is not the best group to ask this question, but I don't know a better one. I'm looking for a *portable* program in C (I mean source code) to detect whether unaligned word access is:...
7
by: Aarti | last post by:
I have written a windows service that needs to perform certain functions only if a User is logged in. The problem I am running into is that I get an error if the computer is in the process of being...
3
by: steve | last post by:
Hi All I need to write a program to read input from barcode scanner. The program is for a gym and needs to detect when a card is scanned regardless of which program is currently running ont he...
0
by: David Eales | last post by:
Hi All, I am having some difficulty when some application hang detection code, I have read the detecting hangs document on msdn but this only works for 1 monitored running app, what I would like...
1
by: wwwords | last post by:
Is there a general method for detecting that a user has changed the record currently visible on a form, whether this is by hitting PgUp or PgDn or clicking on a navigation button, even if no change...
5
Facultas
by: Facultas | last post by:
Right, Im trying to discover whether the machine my program is running on is currently using padding, and just to make sure im trying to find the right thing am i right in saying that padding is...
5
by: NeBlackCat (lists) | last post by:
Hello everybody - my first post! And it may be the most monumentally stupid question ever asked, but I just can't see an answer after several hours experimenting, searching and reading. It's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.