macos 从pid获取应用程序的真实路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7511864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:27:52  来源:igfitidea点击:

Get real path of application from pid?

macosprocessmacos-carbonbsd

提问by RLT

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

如何从进程 ID 中获取进程详细信息,例如应用程序名称和应用程序的真实路径?

I am using Mac OS X.

我正在使用 Mac OS X。

回答by Alen Stojanov

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:

如果您知道 PID,则很容易获得进程名称/位置,只需使用 proc_name 或 proc_pidpath。看看下面的例子,它提供了进程路径:

#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;
}

回答by iwg

You can use the Activity Monitor - http://en.wikipedia.org/wiki/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

PID是您正在寻找的进程 ID 可以找到有关“ps”命令的更多帮助

man ps

回答by hewigovens

Try use lsof

尝试使用 lsof

example:

例子:

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

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

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

输出:
/Users/user/Library/Application Support/Sublime Text 2/Packages

回答by Mecki

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

如果 PID 是“用户应用程序”的 PID,那么您可以NSRunningApplication像这样获取应用程序的 PID :

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 NSRunningApplicationobject allows to to check if the app is ative, to hide/unhide it and do all other kind of neat stuff.

但是,这不适用于系统或后台进程,它仅限于用户应用程序(启动后通常在 Dock 中可见的应用程序)。该NSRunningApplication对象允许检查应用程序是否可用,隐藏/取消隐藏它并执行所有其他类型的整洁工作。

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

只是想我在这里提到它是为了完整性。如果您想使用任意进程,那么接受的答案当然更好。

回答by bunam

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.

我只想在 bash 中制作一个更好的 ssh-copy-id !!为此,我必须知道 sshd 在哪里才能询问他的实际配置。在某些系统上,我有多个 sshd,这不是我的朋友。同样在某些 macOS 上,ps 命令没有显示 sshd 的完整路径。

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

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

所以我必须输入 .ssh/authorized_keys