Linux find process path

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

Источник

Читайте также:  Linux монтировать сетевую папку windows

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!

Читайте также:  Сертификат технической поддержки astra linux special edition

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.

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.

Читайте также:  Extracting using tar in linux

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  

Источник

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