29

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

I am using Mac OS X.

5
  • I did not understand your comment. Can you please elaborate? Commented Sep 22, 2011 at 8:36
  • Could be a duplicate of Finding current executable's path without /proc/self/exe? Commented Sep 23, 2011 at 16:10
  • I dont think its duplicate. Here different application is asking for path based on pid. Commented Sep 23, 2011 at 17:33
  • 5
    Not a duplicate, because OS X doesn't have /proc, only Linux does. Commented Jun 14, 2012 at 19:54
  • 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. Commented Jan 2, 2019 at 6:02

5 Answers 5

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; } 
Sign up to request clarification or add additional context in comments.

6 Comments

URL to Alen's blog entry on this: astojanov.wordpress.com/2011/11/16/…
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. :-)
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.
this works as expected, can someone help on how we can find the "version" of a running process (or say application)
I get segmentation fault when I call proc_pidpath. Not sure what I am doing wrong.
|
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 

4 Comments

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.
@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
@BenLeggiero That is terrible software engineering practice and should be avoided at all costs.
@Qix I agree! That's why I didn't offer it as an answer ;)
12

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

1 Comment

not working on Mac OS 10.12.1. It was lsof -p 22558 -Fn | awk 'NR==5{print}' | sed "s/n\//\//"
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.