views:

75

answers:

5

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?

+1  A: 

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

Ken Aspeslagh
Thanks!! I will check this out. Knew there had to be a better way :)
A: 

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

Dave DeLong
A: 

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.

outis
A: 

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.

0xced
A: 

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

Abizern