观察未知 PID 的进程(无 UI)
我发现了这个问题,但即使接近我需要的,也是无用的
基本上,我有一个应用程序需要在另一个进程(已知名称)启动和/或终止时执行某些操作,但我没有PID,所以我不能设置一个 kqueue 来查找它。
我可以执行“ps aux | grep processtolook | grep -v grep”命令一段时间,但这是我最后的手段。
有什么想法吗?
I've found this question, but even when is close to what I need, is useless
Basically, I have an app that needs to do something when another process (of known name) is launched and/or terminated, but i don't have the PID, so i can't set a kqueue to look for it.
I could do a while for "ps aux | grep processtolook | grep -v grep" command, but that's my last resort.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这个答案:在 Darwin/OSX 中以编程方式确定进程信息。 libproc.h 头文件有 proc_listpids ,它将为您提供所有 pid。然后,您可以在循环中使用 proc_pidinfo 获取 pid 信息并检查名称。按照建议查看顶级来源也可能是值得的。 (当前版本位于此处 http://www.opensource.apple.com /source/top/top-67/。)
不幸的是,这是一个未记录的接口,可能随时更改。此外,这不是最快的事情,因为您必须循环系统中的所有进程。一般来说,这不是一个好方法。
更好的方法可能是编写一个新的 processtolook,它只需调用您可以移动或重命名的旧进程。然后它可以通知您进程的启动和停止。有这种可能吗?
Look at this answer: Determine Process Info Programmatically in Darwin/OSX. The libproc.h header file has
proc_listpids
which will get you all the pids. You can then get the pid information in a loop and usingproc_pidinfo
and check the name. Looking at the top source as suggested there might also be worthwhile. (The current verson is here http://www.opensource.apple.com/source/top/top-67/.)Unfortunately, this is an undocumented interface and subject to change at any time. Also, it isn't the quickest thing, since you have to loop over all the processes in the system. In general, this isn't a great way to do this.
A better way might be to write a new processtolook that simply invoked the old one which you can move or rename. It could then notify you of the process start and stop. Is that a possibility?
如果您想要 PID 的目标进程/程序名称是“processtolook”,那么您可以使用 pidof 命令来获取该正在运行的程序的 PID。
pidof processtolook
查看
pidof
手册。这是 sysvinit-tools 包的一部分。编辑1:
看看我发现的这段代码: http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html
这可能会帮助您了解大纲。
If the target process/program name which you want the PID is "processtolook" then you can use
pidof
command to get the PID of that running program.pidof processtolook
have a look at the
pidof
manual. This is a part ofsysvinit-tools
package.EDIT1:
Have a look at this code i found: http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html
This might help you get you the outline.
在 BSD 上,您可以使用 kvm_openfiles 和 kvm_getprocs 来实现您想要的。我不知道它是否可以在 OSX 上使用。如果您有 pgrep 程序,请查看它是如何实现的(src/bin/pkill.c)。
On BSD you can use kvm_openfiles and kvm_getprocs to implement what you want. I don't know if it's available on OSX. If you have a pgrep program, look how it's implemented (src/bin/pkill.c).