 |
 |
Getting a Process ID
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
I'm building a Cocoa app that uses NSTask to launch a shell script; the shell script, in turn, launches a Java Virtual Machine (JVM) process. I need to get the PID for the JVM process into the Cocoa app.
I can certainly get the PID for the shell script via:
taskPID = [task processIdentifier];
But don't know how to get the PID for the JVM. Any ideas on how I can read the JVM PID into my Cocoa app?
Thanks,

|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
Another approach would be to have my Cocoa app monitor the lifecycle of a Unix process that it did not directly start.
For example,
1. Launch My.app and it would watch for Terminal.app launch events.
2. When I launch the Terminal.app (from the dock) My.app would display a message indicating that Terminal.app was launched along with with its Unix PID.
3. When I quit the Terminal.app, then My.app displays a message indicating that Terminal.app has exited.
Any ideas, on how to approach this scenario?
All I found is good facilities for processes that are launched by a Cocoa application but nothing about monitoring existing processes.
I figure that periodically polling the system through an NSTask that invokes "ps -ax" and then have My.app parse the output for PIDs is inefficient and ugly. There's gotta be a better way, right?

(Last edited by DaGuy; May 26, 2003 at 11:26 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Aug 2000
Location: UK
Status:
Offline
|
|
Originally posted by DaGuy:
All I found is good facilities for processes that are launched by a Cocoa application but nothing about monitoring existing processes.
Look in the NSWorkspace docs:
NSWorkspaceWillLaunchApplicationNotification
NSWorkspaceDidTerminateApplicationNotification
It only works for apps that appear in the Dock, but I think that's what you wanted.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
Originally posted by calumr:
Look in the NSWorkspace docs:
NSWorkspaceWillLaunchApplicationNotification
NSWorkspaceDidTerminateApplicationNotification
It only works for apps that appear in the Dock, but I think that's what you wanted.
Thanks, I wasn't aware of NSWorkspace. As you mentioned (and unfortunately for me) it only picks up dock launched applications.
It would certainly work well for the scenario that I described above but going back to my first post, It's a Java process that I'm really interested in. Perhaps picking the Terminal.app to illustrate my point wasn't very wise. In any event, I added NSWorkspace to my repertoire -and that's a good thing.
Any ideas on how to get the same sort of info (app name, PID) but not just for doc launched processes but for any running process?
Thanks,

|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
It sounds like what you're looking for is sysctl() (declared in sys/sysctl.h). Apple has an example that ought to put you on the right track.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
Originally posted by Chuckit:
It sounds like what you're looking for is sysctl() (declared in sys/sysctl.h). Apple has an example that ought to put you on the right track.
Yes, this is exactly what I've been looking for!
Thanks a whole bunch. I forgot to check for the regular C stuff -a.k.a. Cocoa obsession.

|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|