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

Kramer picture Kramer · Feb 11, 2013 · Viewed 14.3k times · Source

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?

Answer

AlphaMale picture AlphaMale · Feb 11, 2013

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/