问题
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"
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?
回答1:
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}'
We can compare the elapsed time to know the pid of the recently launched one.
来源:https://stackoverflow.com/questions/25389495/how-to-get-the-process-id-pid-of-an-app-launched-using-open-command-on-osx