macos mac os上运行进程的工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8327139/
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:39:14  来源:igfitidea点击:

working directory of running process on mac os

macososx-snow-leopard

提问by Aarkan

I want to know the working directory of a process on Mac OS (10.6). I tried finding the PWD environment variable in ps command's output, but the PWD variable is not available there. Is there a better way to find this for a running process on mac?

我想知道 Mac OS (10.6) 上进程的工作目录。我尝试在 ps 命令的输出中找到 PWD 环境变量,但 PWD 变量在那里不可用。有没有更好的方法可以在 mac 上找到正在运行的进程?

回答by Gordon Davisson

lsof -d cwdwill print the current working directories for all of your processes. If you want to show info on processes you don't own, you need to run it as root (i.e. use sudoas a prefix). If you want to show the info for only certain programs or processes, use e.g. lsof -a -d cwd -c programnameor lsof -a -d cwd -p processid(note: in both cases, the -aflag means that the other flags' restrictions get "and"ed together). lsofis pretty complex and has lots more options, so read its man page for more info.

lsof -d cwd将打印所有进程的当前工作目录。如果您想显示不属于您的进程的信息,您需要以 root 身份运行它(即sudo用作前缀)。如果您只想显示某些程序或进程的信息,请使用 eg lsof -a -d cwd -c programnameor lsof -a -d cwd -p processid(注意:在这两种情况下,该-a标志意味着其他标志的限制被“和”在一起)。 lsof非常复杂,并且有更多选项,因此请阅读其手册页以获取更多信息。

回答by Charphacy

Although Gordon Davisson's answeris great, if you want to do it from code without calling out to lsof, here's the code you need. It's inspired by the lsof source and this blog post.

虽然戈登戴维森的回答很好,但如果你想从代码中做到这一点而不调用lsof,这里是你需要的代码。它的灵感来自 lsof 源代码和这篇博客文章

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>

int main (int argc, char* argv[])
{
        int ret;
        pid_t pid; 
        char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
        struct proc_vnodepathinfo vpi;

        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));
                        return 1;
                }
                printf("proc %d executable: %s\n", pid, pathbuf);
                ret = proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi,
                                   sizeof(vpi));
                if (ret <= 0) {
                        fprintf(stderr, "PID %d: proc_pidinfo ();\n", pid);
                        fprintf(stderr, "    %s\n", strerror(errno));
                        return 1;
                }
                printf("proc %d cwd: %s\n", pid, vpi.pvi_cdir.vip_path);
                // printf("proc %d root: %s\n", pid, vpi.pvi_rdir.vip_path);
        }

        return 0;
}

This sample code will produce output like this:

此示例代码将产生如下输出:

 proc 44586 executable: /bin/zsh
 proc 44586 cwd: /private/tmp

回答by SSteve

If you're talking about doing it within a Cocoa program, this will work:

如果您正在谈论在 Cocoa 程序中执行此操作,这将起作用:

NSFileManager *fm = [[[NSFileManager alloc] init] autorelease];
NSString *currentPath = [fm currentDirectoryPath];