Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > What's up with lsof?

What's up with lsof?
Thread Tools
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 25, 2003, 01:39 AM
 
Hey,
I'm trying to write a script that will tell me what music, of any format, is being played on a computer. I'm using lsof, and iTunes to play the music (aac, and mp3 files) and lsof seems to randomly show the files. I'd like to do

For instance, if I do "lsof | grep iTunes" while playing Cake's "Comfort Eagle", then I get:

Code:
iTunes 507 gtaubman 16r VREG 14,5 5348066 934753 /Users/gtaubman/Music/iTunes/iTunes Music/Cake/Comfort Eagle/05 comfort eagle.mp3
But some other mp3s don't show up. Also, no aac (or m4a as iTunes calls them) show up ever. Is there any way to make this more predictable? Thanks!

gabe

PS, on a side note, what does this mean?:
Code:
27 [Optimus@~]gt % man grep sh: fork: Resource temporarily unavailable /usr/bin/groff:fatal error: fork: Resource temporarily unavailable Error executing formatting or display command. system command exited with status 32768 No manual entry for grep
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
May 25, 2003, 02:23 PM
 
My guess is they don't appear because the files aren't open.

iTunes will typically open a file and read it into ram, playing it from ram and leaving the file closed. Since the file isn't open, lsof doesn't see it. You're probably seeing it sometimes since you're occassionally running the script while iTunes is still reading in the file.

Surprisingly enough, given that iTunes is an Apple app, AppleScript is the easiest way to go with this:

Code:
osascript -e 'tell application "iTunes" to get location of current track'
-->alias Macintosh HD:Users:me:Music:iTunes:iTunes Music:Mazzy Star:So Tonight That I Might See:Fade Into You.m4p

You can get any properties of the track in this way:

If you just wanted the name of the song (not its path):

Code:
osascript -e 'tell application "iTunes" to get name of the current track'
Or how long this track has been playing:
Code:
osascript -e 'tell application "iTunes" to get player position'
length of the current track:
Code:
osascript -e 'tell application "iTunes" to get duration of the current track'
Play with the last two numbers and you can calculate when the next track will start, and so on.
Gods don't kill people - people with Gods kill people.
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
May 25, 2003, 02:26 PM
 
Originally posted by Zimwy:
PS, on a side note, what does this mean?:
Code:
27 [Optimus@~]gt % man grep sh: fork: Resource temporarily unavailable /usr/bin/groff:fatal error: fork: Resource temporarily unavailable Error executing formatting or display command. system command exited with status 32768 No manual entry for grep
This means that the shell wasn't able to fork a new process to run man. This could be from a number of factors including insufficient ram, too many files open, or a corrupt file.

Does it work sometimes and not others? what about after a fresh boot/login?
Gods don't kill people - people with Gods kill people.
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 25, 2003, 11:07 PM
 
Wow, thanks. All that itunes info is great. The only problem is that it's not always itunes that will be playing these files. Sometimes they'll be being streamed from a gnump3d server, or possibly any other form of streaming server. Any ideas for that?

Gabe
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 26, 2003, 07:01 PM
 
Actually, and doing that osascript doesn't work either. I made sure I escaped the quotes and everything, but it still returns nothing. I echo the command it's executing, and then the results, and I get a perfect osascript line, and then NOTHING. If I do it in the terminal, it works fine. Grrrr. I've tried exec, shell_exec, and ``s.

gabe
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
May 27, 2003, 10:32 PM
 
Originally posted by Zimwy:
Actually, and doing that osascript doesn't work either. I made sure I escaped the quotes and everything, but it still returns nothing. I echo the command it's executing, and then the results, and I get a perfect osascript line, and then NOTHING. If I do it in the terminal, it works fine. Grrrr. I've tried exec, shell_exec, and ``s.

gabe
Clearly if it works in the terminal, the script is valid and works.

How are you trying to call the script?
Gods don't kill people - people with Gods kill people.
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
May 28, 2003, 12:38 AM
 
Here's what I do:

[PHP]
$toExec = "osascript -e 'tell application \"iTunes\" to get name of the current track'";
$mp3s = `$toExec`;
echo ($mp3s);
[/PHP]
I've also tried: $mp3s = exec($toExec);
and $mp3s = shell_exec($toExec);
and neither work.

The thing is that if I do
[PHP]
$toExec = "uptime";
$uptime = `$toExec`;
echo ($uptime);
[/php]
then it works fine. Very frustrating.

gabe
     
Forum Regular
Join Date: Feb 2003
Location: Sundsvall, Sweden
Status: Offline
Reply With Quote
Jun 2, 2003, 11:53 PM
 
Originally posted by Zimwy:
Here's what I do:

[PHP]
$toExec = "osascript -e 'tell application \"iTunes\" to get name of the current track'";
$mp3s = `$toExec`;
echo ($mp3s);
[/PHP]
I've also tried: $mp3s = exec($toExec);
and $mp3s = shell_exec($toExec);
and neither work.

The thing is that if I do
[PHP]
$toExec = "uptime";
$uptime = `$toExec`;
echo ($uptime);
[/php]
then it works fine. Very frustrating.

gabe
i would say it doesnt work cus, httpd is running under another user than ure logged in as.

osascript cant send events to apps thats running under a different user than itself, unless u put sudo infront of it,
but for that to work ud have to add the user "www" to /etc/sudoers
but dont do that, cus of big security issues.

however what u could do(i think) is add this to /etc/sudoers

www ALL = NOPASSWD: /usr/bin/osascript

that will allow the www user run osascript as root, without typing a password...

im not a security expert of any kind, so dont blame me if this causes any trouble, im sure there are other ways to do it tho...

good luck
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 06:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2