Get process path linux

How do I get the path of a process in Unix / Linux?

In a 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 that in these environments?

11 Answers 11

On Linux, the symlink /proc//exe has the path of the executable. Use the command readlink -f /proc//exe to get the value.

On AIX, this file does not exist. You could compare cksum and cksum /proc//object/a.out .

You can find the exe easily by these ways, just try it yourself.

This is awesome. I knew I ran it from a location which had the symbolic link to the original executable (one of the many versions). pwdx gave me the location of the symbolic link so I could find the logs and stop the process in proper way.

Last two (pwdx and lsof) may not give you the correct result. The question was about full path to the executable. pwdx and lsof will give you cwd of the process rather than the path to the process. I think the answer of jpalecek is more accurate as the original requestor asked for the path to the executable rather than soft link describing the executable.

This is really useful, however for the last one I seem to need to use lsof -p | grep -m 1 txt , as the required process path info seems to be in the first line with txt , and not in the cwd line? (Applies on macOS and Ubuntu as of date of posting.)

All the answers were specific to Linux.

If you also need Unix, then you need this:

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

Источник

Читайте также:  Права рут пользователя линукс

How to find path from where current process/executable is running?

I am running some executables while connected to a local unix server box. Say, I’m trying to run an executable ‘abc’. Now the server might have provided an alias for ‘abc’.. How do I get to know of this path? As in, if I invoke ‘abc’, it might actually run it from, say, /opt/corp/xyz/abc .. How do I get to know from what path I’m invoking the executable? By the way I’m running on HP-UX 😀

6 Answers 6

» which abc » to show which abc you would be calling

or » alias » to list aliases

perhaps «echo $0» from inside a script, or retrieving argv[0] some other way.

If you are running using the PATH environment variable, you could try:

If there is a symbolic link for the command and you want to know the «real» target, you can use something like:

I do not have access to an HPUX system in front of me right now, but this should work:

$ ls -l /opt/local/bin/wish lrwxr-xr-x 1 root admin 22 Feb 3 21:56 /opt/local/bin/wish@ -> /opt/local/bin/wish8.5 $ readlink /opt/local/bin/wish /opt/local/bin/wish8.5 

If the command is based on an alias, the following will reveal the alias definition.

depending on how your system is configured, the above commands should provide answers to multiple variations of your question.

Does HP-UX have the «which» command? Run:

If you have it, the which command will tell you which abc program will run from your $PATH .

Thanks all! ‘which’ was the commmand I was after! I’m facepalming myself now as I had already known the command (in Ubuntu).. And it does work like a charm in HP-UX!

EDIT : ‘whereis’ suggested by popcnt is even more appropriate! Thanx a lot man!

if you like mine, go ahead and +1 it, and if satisfactory, you could mark it as the answer to your question. That is typically what you’ll see here on SO, instead of an «answer» from the original poster.

From a command line terminal:

The correct way to get the path of a script on Unix is:

Background: $0 is the filename+path of the script relative to the current directory. It can be absolute ( /. ) or relative ( ../ , dir/. ). So the $(dirname «$0») returns the path (without the filename). Mind the quotes; «$0» can contain spaces and other weird stuff.

We then cd into that directory and pwd will return the absolute path where we end up.

In a C program, you should check argv[0] . I’m not sure whether the shell will put the complete path in there. If you have trouble, I suggest to wrap your executable in a small script which prepares the environment and then invoke your executable with:

Источник

Find out current working directory of a running process?

What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.

Читайте также:  Linux ssl root certificate

7 Answers 7

There are 3 methods that I’m aware of:

pwdx

lsof

/proc

Examples

$ pwdx 12136 12136: /home/saml 
$ lsof -p 12136 | grep cwd nautilus 12136 saml cwd DIR 253,2 32768 10354689 /home/saml 

Or you can poke directly into the /proc :

$ readlink -e /proc/12136/cwd/ /home/saml 

In Ubuntu Server 18.04 the above commands require root privileges. At least in OpenBSD, you can see this answer to my U&L question.

I assume that you have the process ID in pid . Most methods on most systems will require that the shell you’re doing this from is running as the same user as the target process (or root).

On Linux and Solaris and perhaps some other System V unices:

On Linux (except embedded systems where readlink is not available) but not Solaris:

On just about any unix variant, you can use lsof . Beware that if there is a newline, it will be printed as \n (indistinguishable from backslash followed by n ). If you feel lucky, you can use the second form, which silently chokes on all whitespace in the directory name.

lsof -a -Fn -p $pid -d cwd | sed -e '1d' -e '2s/^n/' lsof -p $pid | awk '$4=="cwd" ' 

Bonus: if you need to cause a process to change its current directory, you can do it with a debugger. This is useful for example to move a long-running program that doesn’t care about its current directory out of a directory that you want to remove. Not all programs appreciate having their current directory changed under their feet — for example a shell is likely to crash.

#!/bin/sh # Use gdb to change the working directory of a process from outside. # This could be generalized to a lot of other things. if [ $# -ne 2 ]; then echo 1>&2 "Usage: $0 PID DIR" exit 120 fi case "$1" in *[!0-9]*) echo 1>&2 "Invalid pid \`$1'"; exit 3;; esac case "$2" in *[\\\"]*) echo 1>&2 "Unsupported character in directory name, sorry." exit 3;; esac gdb -n -pid "$1" -batch -x /dev/stdin  

Источник

How to check the path which has been a running process?

But this script is running from eg. /home/user/my/program/script.sh So, how I can get the full path of from where the script was running? I have many scripts which name is exactly same, but they are running from different locations and I need to know from where the given script was running. Thanks for reply!

4 Answers 4

for each in `pidof script.sh` do readlink /proc/$each/cwd done 

This will find the pid.s of all script.sh scripts running and find the corresponding cwd (current working directories) for /proc.

use pwdx usage: pwdx pid . (show process working directory) for example,

where 20102 is the pid this will show the process working directory of the process

#!/bin/bash #declare the associative array with PID as key and process directory as value declare -A dirr #This will get the pid of the script pid_proc=($(ps -eaf | grep "$1.sh" | grep -v "grep" | awk '')) for PID in $ do #using Debasish method dirr[$PID]=$(pwdx $PID) # Below are different process to get the CWD of running process # using user1984289 method #dirr[$PID]=$(readlink /proc/"$PID"/cwd) #dirr[$PID]=$(cd /proc/$PID/cwd; /bin/pwd) done # iterate using the keys of the associative and get the working directory for PID in "$" do echo "The script '$1.sh' with PID:'$PID' is in the directory '$'" done 

Use pgrep to get the PIDs of your instances, and then read the link of the associated CWD directory. Basically, the same approach as @user1984289 but using pgrep instead of pidof which does not match bash script names on my system (even with the -x option):

for pid in $(pgrep -f foo.sh); do readlink /proc/$pid/cwd; done 

Just change foo.sh to the name of your script.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43537

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How can I know the absolute path of a running process?

If I have multiple copies of the same application on the disk, and only one is running, as I can see with ps , how can I know the absolute path to distinguish it from the others?

8 Answers 8

% ps -auxwe | grep 24466 root 24466 0.0 0.0 1476 280 ? S 2009 0:00 supervise sshd % sudo ls -l /proc/24466/exe lrwxrwxrwx 1 root root 0 Feb 1 18:05 /proc/24466/exe -> /package/admin/daemontools-0.76/command/supervise

@jarno ls: cannot read symbolic link /proc/28783/exe: Permission denied -- it's not about running the ls command, it's about accessing the process info of a process not belonging to you. On my box, about 97% of all processes listed in /proc are root processes, and the others are distributed over 11 different users.

ls: cannot read symbolic link '/proc/87/exe': No such file or directory lrwxrwxrwx 1 root root 0 Oct 9 07:05 /proc/87/exe

This gives you the current working directory of the pid, not its absolute path.

Usually the which command will tell you which is being invoked from the shell:

@Kokizzu No, it doesn't because it doesn't answer the question at all. The which command only tells you which binary will be run if you execute the command now. The question was "which binary is already running there". Imagine for example having a dozen jdks on your computer. If you want to know for a running java process which jdk it's been taken from, which doesn't help you with that. It will only tell you which jdk it will be taken from, if you execute it now. The accepted answer is also the correct one.

An obvious way this answer is wrong: on my machine I run processes with different JDK versions and some 32bits/64bits. If I want to identify the correct jstack/jmap version for the process the answer above will not work while the accepted answer will.

@Kokizzu This only answers the question, "What is the current working directory of the process $pid ?" The edited post still doesn't answer the question. which merely tells "If the command is on the path, then what is it?"

pwdx return me the absolute path of the exectuable program of the process depending on pid on redhat x64 6.3.

Источник

Оцените статью
Adblock
detector