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?
-
http://superuser.com/questions/103309/how-can-i-know-the-absolute-path-of-a-running-process – Iswanto San Feb 11 '13 at 04:34
-
possible duplicate of [Get real path of application from pid?](http://stackoverflow.com/questions/7511864/get-real-path-of-application-from-pid) – AlphaMale Feb 11 '13 at 04:39
4 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/
One solution can be using which:
which `ps -o comm= -p $PID`
Where $PID is pid of process you want to check. Tested on OS X 10.8.2.
- 1,742
- 1
- 14
- 27
-
2
-
1It seems to return relative paths sometimes though. Here is the test I made: `$> which \`ps -o comm= -p 81233\` => ./ft_minishell2`, I mean, it's not a fullpath as in absolute path. Don't know if that was the initial thinking behind the question. This is Mavericks. – conradkleinespel Mar 02 '14 at 21:17
-
12This may not be accurate if you have an executable with the same name in different locations, esp one that is not on your path. "which" searches your PATH env variable for the give executable, but that may not necessarily be the one showing up in process list. @conradk The reason it is 'relative' is because it is in your current directory. – DustinB Dec 30 '14 at 15:49
-
Anyway to not have a line break with absolute path result? (tested with 10.12.6) – Michael Oct 25 '17 at 18:26
-
@Michael Probably best to remove this via ```sed``` if tou want to use that in script. – tomis Oct 26 '17 at 10:22
-
1
A more portable and modern version of @tomis's answer:
command -v "$(ps -o comm= -p $PID)"
command -v replaces which and is more portable.
Backticks are replaced by $(cmds)
And another alternative that depends on realpath from the GNU CoreUtils:
realpath /proc/$PID/exe
- 14,880
- 4
- 28
- 36
Try this:
sudo ls -l /proc/$(ps -e | grep $PROCESS_NAME | awk '{print $1}')/exe
where $PROCESS_NAME is name of process you want to check.
- 2,167
- 2
- 19
- 39
-
Do you heard about `osxfuse` -- The repository filesystems contains source code for several exciting and useful file systems for you to browse, compile, and build upon, such as sshfs, procfs, AccessibilityFS, etc. – ymn Feb 11 '13 at 05:48
-
2I have heard about that (and using it) but it's not natively there... Many users does not have this installed. – tomis Feb 11 '13 at 05:51