在 Windows 环境中,有一个 API 可以获取正在运行的进程的路径。Unix / Linux中有类似的东西吗?
或者在这些环境中是否有其他方法可以做到这一点?
在 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>
此命令将从执行它的位置获取进程路径。
下面的命令在正在运行的进程列表中搜索进程的名称,并将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
希望这对某人有所帮助......
在 Linux 中,每个进程在/proc
. 因此,您可以使用getpid()
获取正在运行的进程的 pid,然后将其与路径/proc
连接以获取您希望需要的文件夹。
这是 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() 获取当前工作目录。
第二步,如果以上都不成立,就是获取程序名,然后从argv[0]获取程序名,然后从环境中获取用户的PATH,看看有没有合适的具有相同名称的可执行二进制文件。
请注意,argv[0] 是由执行程序的进程设置的,因此它不是 100% 可靠的。
谢谢:
AIX 的Kiwy
:
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);
如果您希望可执行文件的目录可能将工作目录更改为进程的目录(对于媒体/数据/等),您需要删除最后一个 / 之后的所有内容:
*strrchr(buf, '/') = '\0';
/*chdir(buf);*/
查找进程名称的路径
#!/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