![]() |
Terminal: PS results different from Activity Monitor?
Right now, if I look in Activity monitor, my Finder process ID is 97, and its parent ID is 79.
If I look in the Terminal using: ps -w -U myshortname I see that the Finder's PID is 97. So far, so good. Next, I use: ps -j -p 97 and I see that PID 97 is the Finder, but it has a PPID of 73! How can that be? |
How do you check in activity monitor? OsX Version?
|
Using the inspect button. I'm using 10.4.11
Edit: I'm trying to find a way of knowing (in a script) what user is the active user. The script may be running under a user that is logged in but not active. |
What does it say that the name of the parent process is?
Inspect on my Finder gives: loginwindow(20691) and ps agrees. 10.4.11 here also . |
Yes, it does show loginwindow, but ps thinks it belongs to ApplicationServices. I'm logged in as a nonadmin, and I've got an admin user logged in in the background. It's almost as if ps is getting them confused.
|
Just rebooted:
ps -w -U myshortname shows Finder has PID of 97 ps -j -p 97 shows PPID of Finder to be 72 ps -j -p 72 shows process to be ApplicationServices and not loginwindow, which Activity Monitor does (PID: 79) |
Quote:
|
It started out as an exercise for me. Somebody else said they needed it. Now I want to figure out what's wrong with ps as well as know how to tell if a script is running under a user who's in the background.
|
First I believe in ps not activity monitor. Your first finding that Finder was it's own child is definitely a bug.
try this line in terminal and you will see all the relevant process at one time. ps -awwxj|egrep -i "(Fin[d]er|loginw[i]ndow|window[s]erver|P[P]ID)"| cut -c1-21 -c60- Second I can't find any way with ps only to see who is "active". But some combination of looking in /var/log/secure.log and then checking if the last authenticated still has a running loginwindow process should tell you who is "active" this shows the interesting ones of the last 20 lines. cat /var/log/secure.log|tail -20 |grep "com.apple.SecurityServer: authinternal authenticated user" This must be done with some care as the logfiles is rotated weekly? |
I would believe ps, but I noticed in Activity Monitor that the parent process of the Finder for the active user is loginwindow and the parent process for the background user is WindowServer. This seems consistent, at least on the two users I have tried on my system.
|
Quote:
|
Quote:
Code:
windowse 72 1 /System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Resources/WindowServer -daemon |
No I think activity monitor tries to be "helpful" and shows with it believes that you want to see. Yes in your case that is what you want to know but it isn't the parent you see.
One reason I say this is that act. mon. data suggests that the system could "reparent" the process after a switch and I have never heard of a UNIX that does that. |
Quote:
|
Yep exactly the same (10.4.11 here too). ps is consistent whichever user I am. Activity monitor changes its data depending on who I am (hmm do I feel a smell of multiple personality disorder here? ).
|
Test this script:
Code:
#!/bin/bash |
That works. Thanks!
Why the loops? It seems like you should be able to use grep or awk to search the results of ioreg -w0 -Sn Root for the appropriate keys. I tried without success though, so I assume there's more going on than I'm aware of. |
For me, checking the owner of '/dev/console' has been fairly reliable (at least in 10.4 and 10.5) in finding the current active user (root if at the login window), eg.
Code:
stat -f "%Su" /dev/console |
Wow! That is far too easy, but then I guess when you see the answer it's always easy. ;)
|
Quote:
|
Quote:
That's so easy that it borders on cheating. :D |
Quote:
Code:
ioreg -w0 -Sn Root | grep 'kCGSSessionOnConsoleKey"=Yes' | perl -pe 's/kCGSSessionOnConsoleKey"=Yes/\nkCGSSessionOnConsoleKey"=Yes/g' | grep 'kCGSSessionOnConsoleKey"=Yes' | perl -pe 's/kCGSessionLongUserNameKey"="/\n/g' | tail -n 1 | perl -pe 's/"/\n/g' | head -n 1 |
Yes but I'm always careful when data comes on a long line because sometimes order is not guaranteed. With my script order doesn't matter at all.
|
Quote:
|
Example of what I mean:
assume that sometimes kCGSessionLongUserNameKey=... comes before kCGSSessionOnConsoleKey=... your script would then fail. I'm not saying it does but as I can only check on one machine and with it's installed os I can't be sure. Is it perhaps sorted in username order or what? I just prefer to do a real parse if possible unless I have "proof" e.g. a man page that states the order. |
And if we're talking perl then use it the whole way.
Code:
ioreg -w0 -Sn Root |perl -ne '/.*{(.*?kCGSSessionOnConsoleKey"=Yes.*?)}.*/ && $1=~/"kCGSessionLongUserNameKey"="(.*?)"/ && print $1' |
An alternative is to use the 'scutil' command, asking it to show the data for the key "State:/Users/ConsoleUser".
Here's a Perl script that does this (it shows the short username): Code:
#!/usr/bin/perl |
Quote:
Quote:
|
Quote:
Code:
% time ioreg -w0 -Sn Root |perl -ne '/.*{(.*?kCGSSessionOnConsoleKey"=Yes.*?)}.*/ && $1=~/"kCGSessionLongUserNameKey"="(.*?)"/ && print $1' |
Nice one hayne. Do you have any good place to read up on scutil? The man page is rather short.
And on which os doesn't stat work? On tiger it's ok for me at least. |
Quote:
crw------- so only the owner has read permission. And so I thought that 'stat' would fail for anyone other than owner. But I realize now that the relevant permissions are those of the enclosing folder - so all is well. I don't have any better reference on 'scutil' - I agree that the man page is inadequate. Best to just run it interactively and try things: especially 'list' and 'help' |
:D Ahh even you can be mistaken.;)
|
| All times are GMT -5. The time now is 05:46 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.