Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the full path for a process on OS X?

Tags:

I know that I can get the PID for a process by using ps, but how to a find the full path of that process?

like image 931
Kramer Avatar asked Feb 11 '13 04:02

Kramer


People also ask

How do I get full path of application on Mac?

On your Mac, click the Finder icon in the Dock to open a Finder window. Choose View > Show Path Bar, or press the Option key to show the path bar momentarily.

How do I find the process path on a Mac?

Follow the guide on quitting a process and instead of quit, choose the information icon. When you review the open files and ports, the path to the process is near the top of the list.

What is the full path on Mac?

To see it for yourself, open a Finder window, right-click on any of your files, and select Get Info. On the following screen, look for the label that says Where and you will see the full path of your selected file on your Mac. It shows what folders and nested folders your file is located in.


1 Answers

OS X has the libproc library, which can be used to gather different process informations. In order to find the absolute path for a given PID, the following code can be used:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>

int main (int argc, char* argv[])
{
    pid_t pid; int ret;
    char pathbuf[PROC_PIDPATHINFO_MAXSIZE];

    if ( argc > 1 ) {
        pid = (pid_t) atoi(argv[1]);
        ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
        if ( ret <= 0 ) {
            fprintf(stderr, "PID %d: proc_pidpath ();\n", pid);
            fprintf(stderr, "    %s\n", strerror(errno));
        } else {
            printf("proc %d: %s\n", pid, pathbuf);
        }
    }

    return 0;
}

Example to compile and run (above code is stored in pathfind.c, 32291 is the pid of the process I'm trying to find path info for):

$ cc pathfind.c -o pathfind
$ ./pathfind 32291
proc 32291: /some/path/your-binary-app

Refer to this blog post: http://astojanov.wordpress.com/2011/11/16/mac-os-x-resolve-absolute-path-using-process-pid/

like image 119
AlphaMale Avatar answered Oct 22 '22 23:10

AlphaMale