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.

RLT
  • 4,219
  • 4
  • 37
  • 91
  • I did not understand your comment. Can you please elaborate? – RLT Sep 22 '11 at 08:36
  • Could be a duplicate of [Finding current executable's path without /proc/self/exe](http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe)? –  Sep 23 '11 at 16:10
  • I dont think its duplicate. Here different application is asking for path based on pid. – RLT Sep 23 '11 at 17:33
  • 4
    Not a duplicate, because OS X doesn't have /proc, only Linux does. – Quinn Taylor Jun 14 '12 at 19:54
  • Possible duplicate of [Programmatically retrieving the absolute path of an OS X command-line app](https://stackoverflow.com/q/799679/608639). It includes fetching the process pid and then calling `proc_pidpath`. – jww Jan 02 '19 at 06:02

5 Answers5

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;
}
Alen Stojanov
  • 1,178
  • 10
  • 14
  • URL to Alen's blog entry on this: http://astojanov.wordpress.com/2011/11/16/mac-os-x-resolve-absolute-path-using-process-pid/ – Quinn Taylor Jun 14 '12 at 18:18
  • 2
    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. :-) – Quinn Taylor Jun 14 '12 at 18:22
  • 5
    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. – Federico Terzi Apr 03 '20 at 15:25
  • this works as expected, can someone help on how we can find the "version" of a running process (or say application) – Asif Ali Feb 21 '22 at 10:48
  • I get segmentation fault when I call `proc_pidpath`. Not sure what I am doing wrong. – andrea.marangoni Aug 01 '22 at 15:53
  • 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's – Motti Shneor Nov 27 '22 at 17:37
31

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
iwg
  • 498
  • 3
  • 3
  • 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. – ptim Nov 19 '12 at 05:10
  • @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 control – Ky - Oct 30 '18 at 21:52
  • 1
    @BenLeggiero That is terrible software engineering practice and should be avoided at all costs. – Qix - MONICA WAS MISTREATED Nov 13 '18 at 17:22
  • 2
    @Qix I agree! That's why I didn't offer it as an answer ;) – Ky - Nov 13 '18 at 23:03
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

hewigovens
  • 1,190
  • 11
  • 9
  • 3
    not working on Mac OS 10.12.1. It was `lsof -p 22558 -Fn | awk 'NR==5{print}' | sed "s/n\//\//"` – Guy Nov 16 '16 at 11:36
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.

Mecki
  • 125,244
  • 33
  • 244
  • 253
1

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

bunam
  • 21
  • 4