Linux process executable 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.

Читайте также:  File recovery with linux

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:

Источник

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 reliably obtain, programmatically, the executable path corresponding to a process with a given pid, under linux?

The first method seems to be reliable, if combined with method #3. Unfortunately, the path gets a (deleted) suffix when the executable is removed from the system, which can be a suffix part of an ordinary file name. The filter can not be robust if such names are used for executables.

The second method is dependent on the shell that started the process. This is just the first (position 0) argument of the process, and IIUC, shells are free to set it in anyway they see fit. For example, bash prepends dash to login shells.

The third method relies on a name truncated to 15 characters, as taken directly from a field in the kernel task_struct. This is obviously not robust, but is the only name available for kernel processes, and thus must supplement the other two. (Apparently, if the name contains non-ASCII characters they appear as escape sequences, so the method is reliable in this way.)

Altogether, I can not come up with a robust, shell-independent way, to support filtering by process executable name (or ideally path), allowing arbitrary file names. I will probably resort to the leading command parameter in cmdline, since it may fit my purposes, but I would like to make sure that I understand the available options.

Note: Security, although an issue, is a different point. Checking the user identity of the process will be done if security is necessary. But what I desire for the name filter is just correctness. The aim is to implement a quality of service or per-process configuration reliably, and process name filtering will be involved.

Читайте также:  Остановить x server astra linux

There are a few questions with the same general topic (like stackoverflow.com/questions/1023306/…), but the issue of stringent robustness has not been addressed. At least not that I know of.

1 Answer 1

The robustness of the first method (readlink /proc/$PID/exe ) can be improved by doing a pair of stat s on the link itself and the result of the readlink. If you get a matching st_dev and st_ino , they’re the same file. If you don’t get a match, or get an ENOENT , then check for » (deleted)» at the end of the string, strip it off and try again. Repeat until you get a match or run out of » (deleted)» instances.

If you don’t get a match after all that, the executable file really has been deleted. (And you haven’t really specified what you want to do in that case — which you should definitely think about. When you are insisting on robustness, you can’t just ignore the fact that deleted files can be in use!)

There’s still a race condition between the stat s, so you might want to open both files and fstat them instead. Then if you get a device+inode match, you have a file descriptor that can be used with confidence that it actually belongs to the file that was exec’d in the target process, not some other file with a similar name.

The next difficulty is if the process itself goes away during your test, and the PID gets reused. If you care about that, you can read the process start time from /proc/$PID/stat at the beginning and end of the operation, to make sure you were dealing with the same process the whole way through. (Also, there’s a way to keep a process from going away: attach to it as a debugger with ptrace.)

Then there’s the question fo what you want to do if the process execs a different program while you’re looking at it. /proc/$PID/exe will change. If it happens right after your final consistency check, you will return a value that was correct, but isn’t anymore. You can’t do much about that, except the ptrace, and that’s more intrusive than you probably want.

Источник

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