检测其他进程何时关闭 OSX
我需要一种方法来确定 Cocoa 应用程序是否正在运行给定的进程名称。我的一个想法是使用 NSTask 并使用 ps aux | poll grep 进程名.有更好的解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该能够轻松地调整此示例代码以按名称查找进程:
http://developer.apple.com/mac/library/samplecode/PIDFromBSDProcessName/listing1.html
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
如果您使用的是 Snow Leopard,则可以查看 NSRunningApplication 类,特别是 + runningApplicationWithProcessIdentifier: 方法
If you're on Snow Leopard, you could take a look at the NSRunningApplication class particularly the + runningApplicationWithProcessIdentifier: method
我使用了
GetBSDProcessList()
函数 几次都相当成功。I've used the
GetBSDProcessList()
function quite successfully several times.您可以使用流程管理器使用 GetNextProcess 并使用 CopyProcessName 获取进程的名称。请注意,此技术并不能解决观察进程何时启动的问题。
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.
如果您需要检查进程是否运行一段时间(而不是瞬间),请查看 技术说明 2050:无需轮询即可观察进程生命周期。技术说明中的一些技术有 限制:它们只能让您监视当前登录会话中的 GUI 进程。 kqueues<然而, /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.