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?
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With