在Windows环境有一个API得到路运行的一个过程。是否有类似的东西在Unix/Linux?

或者是有一些其他的方式来这样做,在这些环境?

有帮助吗?

解决方案

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

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

其他提示

你可以找到exe很容易地通过这些方式,只是试试它自己。

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

有点晚了,但所有答案都是针对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;
}

已编辑:修正了Mark lakata报告的错误。

我用:

ps -ef | grep 786

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

pwdx <process id>

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

在Linux中,每个进程在/proc中都有自己的文件夹。因此,您可以使用getpid()获取正在运行的进程的pid,然后将其与路径<=>连接,以获取您希望所需的文件夹。

这是Python中的一个简短示例:

import os
print os.path.join('/proc', str(os.getpid()))

以下是ANSI C中的示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>


int
main(int argc, char **argv)
{
    pid_t pid = getpid();

    fprintf(stdout, "Path to current process: '/proc/%d/'\n", (int)pid);

    return EXIT_SUCCESS;
}

用以下内容编译:

gcc -Wall -Werror -g -ansi -pedantic process_path.c -oprocess_path 

有没有"保证在任何地方工作"的方法。

第1步是检查argv[0],如果程序启动通过其全的道路,这会(通常)有充分的道路。如果这是开始通过相对的道路,同拥有的(虽然这需要得到德当前工作的目录,使用getcwd().

步骤2中,如果没有上述的成立,是获得姓名的程序,那么得到该程序的名称从argv[0],则获取用户的路径,从环境和经过,看到,如果有一个适当的可执行的二进制具有相同的名称。

注意argv[0]设过程,执行董事的程序,所以它不是100%可靠的。

谢谢:     Kiwy 点击 使用AIX:

getPathByPid()
{
    if [[ -e /proc/$1/object/a.out ]]; then
        inode=`ls -i /proc/$1/object/a.out 2>/dev/null | awk '{print $1}'`
        if [[ $? -eq 0 ]]; then
            strnode=${inode}"$"
            strNum=`ls -li /proc/$1/object/ 2>/dev/null | grep $strnode | awk '{print $NF}' | grep "[0-9]\{1,\}\.[0-9]\{1,\}\."`
            if [[ $? -eq 0 ]]; then
                # jfs2.10.6.5869
                n1=`echo $strNum|awk -F"." '{print $2}'`
                n2=`echo $strNum|awk -F"." '{print $3}'`
                # brw-rw----    1 root     system       10,  6 Aug 23 2013  hd9var
                strexp="^b.*"$n1,"[[:space:]]\{1,\}"$n2"[[:space:]]\{1,\}.*$"   # "^b.*10, \{1,\}5 \{1,\}.*$"
                strdf=`ls -l /dev/ | grep $strexp | awk '{print $NF}'`
                if [[ $? -eq 0 ]]; then
                    strMpath=`df | grep $strdf | awk '{print $NF}'`
                    if [[ $? -eq 0 ]]; then
                        find $strMpath -inum $inode 2>/dev/null
                        if [[ $? -eq 0 ]]; then
                            return 0
                        fi
                    fi
                fi
            fi
        fi
    fi
    return 1
}

你还可以得到的道路上GNU/Linux与(不彻底的测试):

char file[32];
char buf[64];
pid_t pid = getpid();
sprintf(file, "/proc/%i/cmdline", pid);
FILE *f = fopen(file, "r");
fgets(buf, 64, f);
fclose(f);

如果你想要目录的可执行可能的改变工作目录的进程目录(用于媒体/数据/etc),你需要放下一切之后的最后的/:

*strrchr(buf, '/') = '\0';
/*chdir(buf);*/

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

ps -ef | grep "abc" |grep -v grep| awk '{print $2}' | xargs pwdx

替换<!>“; abc <!>”;用你的特定模式。

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

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

例如:

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

18404: /home2/Avro/NIFI

希望这可以帮助某人.....

找到进程名称的路径

#!/bin/bash
# @author Lukas Gottschall
PID=`ps aux | grep precessname | grep -v grep | awk '{ print $2 }'`
PATH=`ls -ald --color=never /proc/$PID/exe | awk '{ print $10 }'`
echo $PATH
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top