Linux process file path

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.

Источник

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 programs for mac

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

Источник

The current path of a process

How does the OS assign and change the current path for a process, during it's run-time?

I'm not really sure what do you mean by current path. Take a look at chdir(2), perhaps it's what you look for.

The initial directory a program runs in its father's current directory. For example, if you launch a program from a shell, the shell's current directory will be the initial directory of the program. Does that answer (partially) your question or are you looking for something else?

@lgeorget:thanks, that answer the first question. The second question, for example, when you run an emacs process, and open in emacs a file in path3 , why is the current dir changes to path3 ?

Actually, it might not be changed. A process can create or open a file inother place than its current directory. I'll post a proper answer.

1 Answer 1

Question 1 : Why is the directory where a program is installed not the initial directory of the process when running the program?

Actually, the installation path of a program is irrelevant. What matters is the current path of the father process. In case of a program launched from a shell, the father process is the shell itself so the initial current directory of the new process is the shell's current directory.

Question 2 : How can a process create a file outside from its current directory?

There are two ways to give the path of a file: absolute path and relative path. An absolute path is interpreted from the root of the filesystem ( / ) and start with a slash ("/"). A relative path is interpreted from the current directory of the process. So if you have two directories, for example /path2 and /path2/path3 , and a process whose current directory is path2 , it can open a file path3/file . This path is relative (it doesn't start with a slash) so it's computed from the current directory path2 . And finally, the new file's complete path is /path2/path3/file . So a process running in a given directory may create file outside of it.

Question 3 : How does the OS assign and change the current path for a process, during its running?

A process can ask the OS for changing its current directory by the mean of the chdir(2) system call (provided that it has needed permissions on the new directory for it, etc. etc.). That's a different mechanism which has nothing to do with opening files. Opening files is done through another system call (namely open(2) ).

Источник

Linux process file path

Use the ps command to view the startup location of a process file under Linux

Use the ps command, the usage method is as follows: Shutdown is the shutdown command, but what you see at this time is only a relative path, not an absolute path, such as: 4170 is the process I.

Use the ps command to view the startup location of a process file under Linux

Use the ps command, the usage method is as follows: Shutdown is the shutdown command, but what you see at this time is only a relative path, not an absolute path, such as: 4170 is the process ID, ente.

[Linux] Use TCPDUMP to view the TCP transmission process during the upload file

The following is the transfer process of TCP when it is uploaded image files. Three handshake processes Client ===> Server [S] flag SYN is 1, MSS 65495 (maximum data part of each package is .

[Linux] View project run port number, process number, file location

First, check if the project is running, ie the PID number ​ PS-EF | GREP project name (process name) orPS -AUX | GREP project name (process name),as follows: In the above query results,2000995Be runni.

Linux ps view process

USER Running user PID process VSZ virtual memory size RSS real size TTY where to start STAT process status S dormant s main process

Источник

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