在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
pwdx <PID>
给了我符号链接的位置,所以我可以找到日志并以正确的方式停止进程。 - NurShomikll
通常是一个别名:alias ll='ls -alF'
。 - Pablo Bianchilsof -p <PID> | grep -m 1 txt
,因为所需的进程路径信息似乎在第一行中带有 txt
,而不是在 cwd
行中?(适用于发布日期时的 macOS 和 Ubuntu。) - MikeBeaton所有答案都是针对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 on 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 on Linux, no guarantee */
/* check if we have something like execve("foobar", NULL, NULL) */
if (argv0 == NULL)
{
/* We surrender and give the 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 to use execve: we don’t know the executable name; we surrender and instead give the current path */
if (getcwd (path, dest_len - 1) == NULL)
return NULL;
strcat (path, "/");
return path;
}
"/proc/self/exe"
替换为 sprintf(foo,"/proc/%d/exe",pid)
。 - Mark Lakata我使用:
ps -ef | grep 786
用你的PID或进程名称替换786。
pwdx <进程 ID>
该命令将获取正在执行的进程路径。
ps -ef | grep "abc" |grep -v grep| awk '{print $2}' | xargs pwdx
ps1() { ps -ef | grep "$1" |grep -v grep| awk '{print $2}' | xargs pwdx; }
例如:
[admin@myserver:/home2/Avro/AvroGen]$ ps1 nifi
18404: /home2/Avro/NIFI
import os
print os.path.join('/proc', str(os.getpid()))
#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
- Luke Stanleyimport os print os.path.join('/proc', str(os.getpid())) /proc/24346
对于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
}
Courtesy Kiwy.
你也可以在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);*/