如何在Unix/Linux中获取进程的路径

lsa*_*mon 126 unix linux environment process path

在Windows环境中,有一个API来获取正在运行进程的路径.在Unix/Linux中有类似的东西吗?

或者在这些环境中还有其他方法吗?

jpa*_*cek 168

在Linux上,符号链接/proc/<pid>/exe具有可执行文件的路径.使用该命令readlink -f /proc/<pid>/exe获取值.

在AIX上,此文件不存在.你可以比较cksum <actual path to binary>cksum /proc/<pid>/object/a.out.

  • `sudo`如果输出为空,则某些进程由其他系统用户创建. (2认同)

hah*_*ile 52

您可以通过这些方式轻松找到exe,只需自己尝试一下.

  • ll /proc/<PID>/exe
  • pwdx <PID>
  • lsof -p <PID> | grep cwd

  • `ll` 通常是一个别名:`alias ll='ls -alF'`。 (2认同)

Hip*_*ion 27

有点晚了,但所有的答案都是针对linux的.

如果你还需要unix,那么你需要这个:

char * getExecPath (char * path,size_t dest_len, char * argv0)
{
    char * baseName = NULL;
    char * systemPath = NULL;
    char * candidateDir = NULL;

    /* the easiest case: we are in linux */
    size_t buff_len;
    if (buff_len = readlink ("/proc/self/exe", path, dest_len - 1) != -1)
    {
        path [buff_len] = '\0';
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Ups... not in linux, no  guarantee */

    /* check if we have something like execve("foobar", NULL, NULL) */
    if (argv0 == NULL)
    {
        /* we surrender and give current path instead */
        if (getcwd (path, dest_len) == NULL) return NULL;
        strcat  (path, "/");
        return path;
    }


    /* argv[0] */
    /* if dest_len < PATH_MAX may cause buffer overflow */
    if ((realpath (argv0, path)) && (!access (path, F_OK)))
    {
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Current path */
    baseName = basename (argv0);
    if (getcwd (path, dest_len - strlen (baseName) - 1) == NULL)
        return NULL;

    strcat (path, "/");
    strcat (path, baseName);
    if (access (path, F_OK) == 0)
    {
        dirname (path);
        strcat  (path, "/");
        return path;
    }

    /* Try the PATH. */
    systemPath = getenv ("PATH");
    if (systemPath != NULL)
    {
        dest_len--;
        systemPath = strdup (systemPath);
        for (candidateDir = strtok (systemPath, ":"); candidateDir != NULL; candidateDir = strtok (NULL, ":"))
        {
            strncpy (path, candidateDir, dest_len);
            strncat (path, "/", dest_len);
            strncat (path, baseName, dest_len);

            if (access(path, F_OK) == 0)
            {
                free (systemPath);
                dirname (path);
                strcat  (path, "/");
                return path;
            }
        }
        free(systemPath);
        dest_len++;
    }

    /* again someone has use execve: we dont knowe the executable name; we surrender and give instead current path */
    if (getcwd (path, dest_len - 1) == NULL) return NULL;
    strcat  (path, "/");
    return path;
}
Run Code Online (Sandbox Code Playgroud)

编辑:修正了Mark lakata报道的错误.

  • 请注意,readlink不会终止结果,因此此代码具有未定义的行为. (2认同)

Use*_*ser 12

我用:

ps -ef | grep 786
Run Code Online (Sandbox Code Playgroud)

将786替换为您的PID或进程名称.


gob*_*obi 9

pwdx <process id>

此命令将从执行位置获取进程路径.


小智 5

下面的命令在正在运行的进程列表中搜索进程的名称,并将pid重定向到pwdx命令以查找进程的位置。

ps -ef | grep "abc" |grep -v grep| awk '{print $2}' | xargs pwdx
Run Code Online (Sandbox Code Playgroud)

将“abc”替换为您的特定模式。

或者,如果您可以将其配置为.bashrc中的函数,那么如果您需要经常使用它,您可能会发现很方便。

ps1() { ps -ef | grep "$1" |grep -v grep| awk '{print $2}' | xargs pwdx; }
Run Code Online (Sandbox Code Playgroud)

例如:

[admin@myserver:/home2/Avro/AvroGen]$ ps1 nifi

18404: /home2/Avro/NIFI
Run Code Online (Sandbox Code Playgroud)