Linux find process executable

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 *where* the executable is for a currently running process

If I have 2 executables ( /path/to/gcc/myapp.exe , and /path/to/intel/myapp.exe ) where one is compiled with an intel compiler and one is compiled with standard gcc, running both will show up in top as just myapp . If I run both of those and have processes running as myapp , is there a way I can find out which process is coming from which executable? We have a bunch of apps installed on our cluster and I am interested, not only in which apps are being used the most, but which versions of the apps are being used. If I see 500 occurences of R being run, I would like to know if 100 of those are from /path/to/R-3.2.1/intel/R and the other 400 are from /path/to/R-3.1.3/gcc/R , etc. How can I do this?

Читайте также:  Linux file server project

3 Answers 3

You can do this with the /proc filesystem, as noted in the proc(5) manual page:

/proc/[pid]/exe

Under Linux 2.2 and later, this file is a symbolic link containing the actual pathname of the executed command.

Because it is a symbolic link, you can read the target (do an ls -l to see the pathname).

The [pid] of course means a process-ID for the particular executable you are interested in, at runtime.

(It is unlikely that you have a system running a Linux kernel that old, so «always works» should be good enough).

To get a count, you simply need a script that walks through /proc , for each directory which you own (since you are running the programs, you can see their /proc data), and matching the pathnames against the ones you are interested int. The readlink program, for instance, is useful in bash scripts (and since it is part of coreutils , you likely have it on a Linux system).

Источник

If I see a process running using ps, how can I find the executable?

On Linux: if you know the PID, you can cat the cmdline file for that file. E.g.:

This will probably fail if the binary was moved after the program was started.

lsof -n | grep PID | grep ' txt ' 

which is a symbolic link to the executable.

Note that /proc/PID/cmdline doesn’t have a newline character, so you’ll probably want to do something like cat /proc/PID/cmdline ; echo » .

Actually, it has NUL characters separating the arguments, so you might want something even more elaborate like tr ‘\0’ ‘ ‘ < /proc/PID/cmdline ; echo ''

Copy the process id from ps -e command and then run the following:

None of the methods ( ls , lsof or cat ) in the other answers work for me. If I do:

$ pgrep -f -l test 3074 nano test.txt 

Or, in order to obtain only the PID to use it in programming:

Tested on Kali Linux v1.0.6 (Debian based).
Compared to a simple ls , I must admit it is not a so portable solution, but at least it works.

You must log in to answer this question.

Linked

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.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

Читайте также:  Linux port forwarding to any port

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 do I search for a process by name without using grep?

OP just wants a shorter command I’d guess. If there would be a thing like ps -n that would answer his need. ps -n firefox is a bit shorter than ps | grep firefox . ps can already filter on pid or processes for a user id, so it’s a reasonable question to filter on process name.

8 Answers 8

The pgrep command, and its sibling pkill , exists precisely for this purpose:

  • pgrep firefox will list all processes whose commands match firefox
  • pgrep -f firefox will list all processes whose entire command lines match firefox
  • pgrep -x firefox will list all processes whose commands exactly match firefox
  • . and so on.

And naturally, pgrep will exclude itself from the match, so none of the grep rituals associated with ps | grep are needed.

The other set of tools for this are the pidof and killall commands. These aren’t as flexible as pgrep and pkill .

Useful if you have a long command name and/or command line args is pgrep -f foo which uses the full process name to match foo

 -C cmdlist Select by command name. This selects the processes whose executable name is given in cmdlist. -f Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the format keyword comm. 

This is the best answer but unfortunately doesn’t work on OSX. The BSD ps -C flag behaves completely differently — «Change the way the CPU percentage is calculated»

You will get all the processes with names

exmple: 1747 568 568 ? 00:00:00 colord 1833 1832 1832 ? 00:00:00 gnome-keyring-d 2263 568 568 ? 00:00:00 udisksd 2311 2311 2311 ? 00:00:00 cupsd 2315 2315 2311 ? 00:00:00 dbus 

Redirect or so copy the output to a file and then open nano , press Ctrl + W and you can search for the name you want.

top allows you to search for string when you hit uppercase L ; the process will be highlighted, and use up and down arrow keys to scroll through list of processes. Similarly, htop command allows highlighting a particular process when you hit / . And \ will filter all the processes with a particular string in the name.

For those who like awk, here’s an awk oneliner: ps -eF | awk ‘/process-name/ ‘ . With ps -eF process name is always in 11th column. Alternatively if you do ps -eF | awk » | sort you get a sorted list of processes names, sorted alphabetically. Pipe it into less command just to view the long list of files easier.

Читайте также:  What is lsusb in linux

Источник

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:

Источник

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