回答
在Linux上,符號鏈接/proc/<pid>/exe
具有可執行文件的路徑。使用命令readlink -f /proc/<pid>/exe
來獲取該值。
在AIX上,該文件不存在。你可以比較cksum <actual path to binary>
和cksum /proc/<pid>/object/a.out
。
在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
不錯,但我需要C ANSI的使用。坦克 – lsalamon 2009-03-03 12:25:31
在最近版本的Ubuntu上輸出Python: >>> import os >>> print os.path.join('/ proc',str(os.getpid())) /proc/24346 – 2012-05-26 18:20:42
有沒有「保證:
#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%的可靠的處理。
編者注下面的代碼有一個錯誤。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;
}
謝謝爲分享Hiperion,但我需要指定一個PID並獲得它的EXE路徑,這是可能的代碼? – Noitidart 2016-10-22 16:36:26
@Noitidart - 用`sprintf(foo,「/ proc /%d/exe」,pid)替換``/ proc/self/exe``` – 2016-11-03 19:00:34
請注意readlink不會終止結果,所以這段代碼有未定義的行爲。 – 2016-11-03 22:22:16
找到路徑的進程名
#!/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
您還可以得到在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);*/
您可以通過以下方式輕鬆找到EXE,只是自己嘗試一下。
ll /proc/<PID>/exe
pwdx <PID>
lsof -p <PID> | grep cwd
我用:
ps -ef | grep 786
更換786與您的PID號或進程名。
感謝: 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
}
pwdx <process id>
此命令將取從它正在執行,其中處理路徑。
- 1. C++ Windows如何從PID中獲取進程路徑
- 2. 按執行路徑獲取進程
- 3. 如何在AngularJS中獲取當前路徑的路徑名?
- 4. 如何在C#/ Win32中獲取正在運行的進程的DOS路徑名?
- 5. 如何在ASP.NET中獲取路徑名?
- 6. 如何在JavaScript中獲取根路徑?
- 7. 如何在xml中獲取路徑
- 8. 如何獲取R.java路徑?
- 9. 如何獲取Groovy中的類路徑?
- 10. 如何獲取%CD的父路徑%如何獲取當前%CD%的父路徑%
- 11. 如何在iOS中獲取目錄的路徑應用程序
- 12. 如何從相對路徑中獲取我的Perl程序的絕對路徑?
- 13. 獲取正在運行的進程的目錄路徑ID
- 14. 按名稱獲取正在運行的進程的路徑
- 15. Dropwizard路徑:獲取應用程序中的所有路徑
- 16. 如何在C中獲取應用程序路徑?
- 17. 如何獲取C#中安裝程序類中的msi路徑
- 18. 如何從HDFS路徑中獲取絕對路徑
- 19. 如何從Perl獲取我的路徑中的程序列表?
- 20. 如何獲取ggmap路徑數據以跟蹤道路路徑
- 21. 如何獲取winforms中的應用程序設置路徑?
- 22. 如何獲取winforms應用程序中的文件路徑
- 23. 如何獲取MFC中的應用程序當前路徑(VC++)?
- 24. 如何獲取asp.net中的應用程序路徑?
- 25. 如何在Eclipse插件中獲取IJavaProject的類路徑作爲文件路徑?
- 26. 如何在.net中獲取快捷方式(不是目標路徑)的路徑?
- 27. 如何獲取CalCalendarItem的文件路徑?
- 28. 如何獲取URL的路徑
- 29. 如何獲取dll的相對路徑?
- 30. 如何獲取py文件的路徑?
`sudo`如果輸出爲空,則某些進程由其他系統用戶創建。 – Lun4i 2017-09-02 05:21:46