如何获取在OSX上使用“打开"命令启动的应用程序的进程ID(PID)? [英] How to get the process id (PID) of an app launched using `open` command on OSX?
问题描述
我已经通过OSX命令行上的open
命令启动了一个应用程序,如下所示:
I've launched an application via the open
command on the OSX command line like so:
open -a "/Applications/Adobe After Effects CC/Adobe After Effects CC.app"
我想获取该启动的应用程序的进程ID.有什么方法可以在OSX上可靠地做到这一点吗?看来open
不会返回任何内容,所以我不确定我什至可以将其结果传递给类似ps
的内容来执行grep
操作.我以为也许是因为该应用程序是通过终端启动的,所以我会知道哪个应用程序是frontmost
,但是我对该解决方案的可靠性表示怀疑.有什么想法吗?
I want to get the process id of that launched application. Is there any way to do this reliably on OSX? It doesn't seem open
returns anything, so I'm not sure I can even pipe its result into something like ps
to perform a grep
operation. I thought that maybe since the app is launched via the terminal I would know which app is the frontmost
, but am doubting the reliability of that solution. Any ideas?
推荐答案
执行open -a之后,可以执行ps命令.在ps命令的输出上执行grep会提供有关进程ID的信息.
After executing open -a, you can execute ps command. Doing a grep on the output of ps command gives info on process ID.
ps aux | grep -v grep |grep -i <application name> | awk '{print $2;}'
下面给出的一个给出了该过程所花费的时间.
The one given below gives the elapsed time for the process.
ps aux -o etime,command | grep -v grep |grep -i <application name> | awk '{print $2, $12,$13}'
我们可以比较经过的时间来了解最近启动的pid.
We can compare the elapsed time to know the pid of the recently launched one.
这篇关于如何获取在OSX上使用“打开"命令启动的应用程序的进程ID(PID)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!