Mike Hornblade
Mike Hornblade

Reputation: 1584

Detect when other process is shutdown OSX

I need a way to determine from a Cocoa app if a given process name is running. One idea I had was to use NSTask and poll using ps aux | grep processName. Is there a better solution?

Upvotes: 2

Views: 405

Answers (5)

outis
outis

Reputation: 77400

If you need to check that a process is running for a duration (rather than at an instant), take a look at Tech Note 2050: Observing Process Lifetimes Without Polling. Some of the techniques in the tech note have limitations: they only let you monitor GUI processes in the current login session. kqueues, however, might be exactly what you're looking for: they let you monitor one process of any kind.

Upvotes: 0

Abizern
Abizern

Reputation: 150595

If you're on Snow Leopard, you could take a look at the NSRunningApplication class particularly the + runningApplicationWithProcessIdentifier: method

Upvotes: 1

0xced
0xced

Reputation: 26478

You can use the Process Manager API to loop through all the running processes with GetNextProcess and use CopyProcessName to get the name of the process. Note that this technique does not solve the problem of observing when a process is launched.

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243146

I've used the GetBSDProcessList() function quite successfully several times.

Upvotes: 0

Ken Aspeslagh
Ken Aspeslagh

Reputation: 11594

You should be able to easily adapt this sample code to look for a process by name:

http://developer.apple.com/mac/library/samplecode/PIDFromBSDProcessName/listing1.html

Upvotes: 1

Related Questions