In Windows environment there is an API to obtain the path which is running a process. Is there something similar in Unix / Linux?
Or is there some other way to do th
I use:
ps -ef | grep 786
Replace 786 with your PID or process name.
You can find the exe easily by these ways, just try it yourself.
ll /proc/<PID>/exe
pwdx <PID>
lsof -p <PID> | grep cwd
pwdx <process id>
This command will fetch the process path from where it is executing.
Find the path to a process name
#!/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
thanks :
Kiwy
with 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
}
In Linux every process has its own folder in /proc
. So you could use getpid()
to get the pid of the running process and then join it with the path /proc
to get the folder you hopefully need.
Here's a short example in Python:
import os
print os.path.join('/proc', str(os.getpid()))
Here's the example in ANSI C as well:
#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;
}
Compile it with:
gcc -Wall -Werror -g -ansi -pedantic process_path.c -oprocess_path