Get real path of application from pid?
Asked Answered
L

5

26

How can I get the process details like name of application & real path of application from process id?

I am using Mac OS X.

Lindsley answered 22/9, 2011 at 8:30 Comment(5)
I did not understand your comment. Can you please elaborate?Lindsley
Could be a duplicate of Finding current executable's path without /proc/self/exe?Septicidal
I dont think its duplicate. Here different application is asking for path based on pid.Lindsley
Not a duplicate, because OS X doesn't have /proc, only Linux does.Cantlon
Possible duplicate of Programmatically retrieving the absolute path of an OS X command-line app. It includes fetching the process pid and then calling proc_pidpath.Masseter
B
39

It's quite easy to get the process name / location if you know the PID, just use proc_name or proc_pidpath. Have a look at the following example, which provides the process path:

#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;
}
Bratton answered 16/11, 2011 at 9:16 Comment(6)
URL to Alen's blog entry on this: astojanov.wordpress.com/2011/11/16/…Cantlon
This worked perfectly for me, thanks Alen! Surprising how hard it is to find this information online --- all the Linux heads insist that /proc should work. :-)Cantlon
A note about the proc_pidpath function: the buffer size MUST be PROC_PIDPATHINFO_MAXSIZE. If you specify a smaller size, the call fail, even though the dimension would be enough to fit the complete path.Pastorship
this works as expected, can someone help on how we can find the "version" of a running process (or say application)Gavotte
I get segmentation fault when I call proc_pidpath. Not sure what I am doing wrong.Aspect
I wonder how this proc_path low-level buffer handles paths that are employing strange Unicode names in the directories and binary file-names and how one can interpret this buffer on MacOS. After all you can build a program whose (executable) name contains diacrticals, Chinese, Arabic, Hebrew, Emojis or a combination of the all of them. It is pretty strange that no high-level (Cocoa/Foundation) level API exists for doing this outside of the Bundle and NSRunningApplication API'sNoontime
I
32

You can use the Activity Monitor - http://en.wikipedia.org/wiki/Activity_Monitor

Or in the Terminal App you can use:

ps xuwww -p PID

PIDis the process id you are looking for More help on 'ps`command you can find with

man ps
Irizarry answered 22/9, 2011 at 8:59 Comment(4)
Is this downvoted because the app name is not returned? ps [PID] worked for me when I needed it just now - I did have to infer the name of the application from the path, admittedly.Trifling
@Qix it can be by using Process (AKA NSTask), setting its launch path to /bin/bash, its arguments to ["-c", "ps xuwww -p \(pid)"], and its standardOutput to a Pipe you controlLyndsaylyndsey
@BenLeggiero That is terrible software engineering practice and should be avoided at all costs.Lael
@Qix I agree! That's why I didn't offer it as an answer ;)Lyndsaylyndsey
T
11

Try use lsof

example:

lsof -p 1066 -Fn | awk 'NR==2{print}' | sed "s/n\//\//"

output:
/Users/user/Library/Application Support/Sublime Text 2/Packages

Tripos answered 10/5, 2013 at 2:53 Comment(1)
not working on Mac OS 10.12.1. It was lsof -p 22558 -Fn | awk 'NR==5{print}' | sed "s/n\//\//"Protuberancy
S
5

If the PID is the PID of a "user application", then you can get the NSRunningApplication of the app like that:

NSRunningApplication * app = [NSRunningApplication  
    runningApplicationWithProcessIdentifier:pid
];

And to print the path of the executable:

NSLog(@"Executable of app: %@", app.executableURL.path);

the app bundle itself is here

NSLog(@"Executable of app: %@", app.bundleURL.path);

However this won't work with system or background processes, it's limited to user apps (those typically visible in the dock after launch). The NSRunningApplication object allows to to check if the app is ative, to hide/unhide it and do all other kind of neat stuff.

Just thought I mention it here for completeness. If you want to work with arbitrary processes, then the accepted answer is of course better.

Subclavian answered 8/9, 2015 at 14:44 Comment(0)
V
0

I would like to make a better ssh-copy-id in bash only!! For that, i have to know where is sshd to ask him his actual config. On some system i have multiple sshd and which is not my friend. Also on some macOS the ps command didn't show the full path for sshd.

lsof -p $PPID | grep /sshd | awk '{print $9}'

this return

/usr/sbin/sshd

after i could ask for

sudo /usr/sbin/sshd -T | grep authorizedkeysfile

this return, on some system

authorizedkeysfile .ssh/authorized_keys

so i have to put in .ssh/authorized_keys

Vanatta answered 28/12, 2016 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.