2009-03-03 166 views

回答

124

在Linux上,符號鏈接/proc/<pid>/exe具有可執行文件的路徑。使用命令readlink -f /proc/<pid>/exe來獲取該值。

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

+1

`sudo`如果輸出爲空,則某些進程由其他系統用戶創建。 – Lun4i 2017-09-02 05:21:46

4

在Linux中,每個進程都有自己的文件夾/proc。因此,您可以使用getpid()獲取正在運行的進程的pid,然後將其與路徑/proc結合以獲取您希望需要的文件夾。

這裏是Python中的小例子:

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

下面是ANSI C的例子還有:

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

不錯,但我需要C ANSI的使用。坦克 – lsalamon 2009-03-03 12:25:31

+0

在最近版本的Ubuntu上輸出Python: >>> import os >>> print os.path.join('/ proc',str(os.getpid())) /proc/24346 – 2012-05-26 18:20:42

2

有沒有「保證:

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

與編譯在任何地方工作「

第1步是檢查argv [0],如果程序是由其完整路徑啓動的,則通常會有完整路徑。如果它是由一個相對路徑啓動的,這同樣適用(儘管這需要使用getcwd()來獲取當前工作目錄。

步驟2,如果以上都不成立,則獲取程序的名稱,然後從argv [0]獲取程序的名稱,然後從環境中獲取用戶的PATH並查看是否有合適的可執行二進制文件具有相同的名稱。

請注意,設置了argv [0]由高層程序,所以它不是100%的可靠的處理。

26

編者注下面的代碼有一個錯誤。readlink確實不是n你會終止輸出字符串,所以如果它起作用,那是一個意外。

有點晚,但所有的答案都是特定於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 */ 
    if (readlink ("/proc/self/exe", path, dest_len) != -1) 
    { 
     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; 
} 
+0

謝謝爲分享Hiperion,但我需要指定一個PID並獲得它的EXE路徑,這是可能的代碼? – Noitidart 2016-10-22 16:36:26

+1

@Noitidart - 用`sprintf(foo,「/ proc /%d/exe」,pid)替換``/ proc/self/exe``` – 2016-11-03 19:00:34

+1

請注意readlink不會終止結果,所以這段代碼有未定義的行爲。 – 2016-11-03 22:22:16

-1

找到路徑的進程名

#!/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 
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); 

如果你想要的可執行文件的目錄可能會改變工作目錄到進程小號的目錄(媒體/數據/等),則需要在最後/放下一切:

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

您可以通過以下方式輕鬆找到EXE,只是自己嘗試一下。

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

我用:

ps -ef | grep 786 

更換786與您的PID號或進程名。

2

感謝: 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 
} 
0

pwdx <process id>

此命令將取從它正在執行,其中處理路徑。

相關問題