Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the process id (PID) of an app launched using `open` command on OSX?

Tags:

terminal

macos

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?

like image 402
ariestav Avatar asked Aug 19 '14 17:08

ariestav


People also ask

How do I find the PID of a process on a Mac?

One very useful command to help find a process by name or PID is grep which can filter out the desired information. It can be used in conjunction with the ps -ax command to list only the process that you are interested in. For example: At the command prompt type ps -ax | grep <application name>.

How do I find the PID number of a process?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

How do I find the PID of a process in terminal?

A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


2 Answers

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.

like image 129
Seema Kadavan Avatar answered Dec 06 '22 12:12

Seema Kadavan


These days, pgrep -n $APPLICATION_NAME seems like the easiest way to accomplish this. From the man page:

-n    Select only the newest (most recently started) of the matching processes.
-o    Select only the oldest (least recently started) of the matching processes.
like image 34
Carter Avatar answered Dec 06 '22 12:12

Carter