简体   繁体   English

从pid获取真正的应用路径?

[英]Get real path of application from pid?

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.

It's quite easy to get the process name / location if you know the PID, just use proc_name or proc_pidpath. 如果您知道PID,那么获取进程名称/位置非常容易,只需使用proc_name或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;
}

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

PID is the process id you are looking for More help on 'ps`command you can find with PID是您正在寻找的进程ID。有关'ps`命令的更多帮助,您可以找到它

man ps

Try use lsof 尝试使用lsof

example: 例:

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

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

If the PID is the PID of a "user application", then you can get the NSRunningApplication of the app like that: 如果PID是“用户应用程序”的PID,那么您可以获得应用程序的NSRunningApplication

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). 但是,这不适用于系统或后台进程,它仅限于用户应用程序(通常在启动后在Dock中可见)。 The NSRunningApplication object allows to to check if the app is ative, to hide/unhide it and do all other kind of neat stuff. NSRunningApplication对象允许检查应用程序是否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. 如果你想使用任意进程,那么接受的答案当然更好。

I would like to make a better ssh-copy-id in bash only!! 我想在bash中做一个更好的ssh-copy-id !! For that, i have to know where is sshd to ask him his actual config. 为此,我必须知道sshd在哪里向他询问他的实际配置。 On some system i have multiple sshd and which is not my friend. 在某些系统上,我有多个sshd,这不是我的朋友。 Also on some macOS the ps command didn't show the full path for sshd. 另外在某些macOS上,ps命令没有显示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 所以我必须放入.ssh / authorized_keys

Use this code to find the processinfo psn. 使用此代码查找processinfo psn。

Then use following function to get location, 然后使用以下函数获取位置,

OSStatus GetProcessBundleLocation (const ProcessSerialNumber *psn, FSRef *location);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM