How to know the path of a running executable

executablefilesprocess

I am working on a Linux server, and I running different jobs on different node.

However, when compiling my program, I didn't set their specific name, so they are all a.out

Now I found one of the running a.out, may be not right, and want to terminated. But the Top command doesn't show the path to the executable.

enter image description here

How to do it?

Best Answer

You can use lsof (available for just about any Unix variant, but often not part of the default installation) to list all the files a process is using. “Using” includes open file descriptors as well as closely related concepts such as which executable the process is running. The executable has txt in the FD column, for obscure historical reasons.

$ lsof -p1234 | grep txt
a.out    1234 user15964  txt   REG  253,0  34567 /path/to/a.out

(made-up output)

On Solaris and Linux, there's a more direct way: the proc filesystem provides information about each process, including which executable it's running. (On Linux at least, that's where lsof gets its information.)

$ ls -l /proc/1234/exe
lrwxrwxrwx 1 root root 0 Feb 30 34:56 /proc/1234/exe -> /path/to/a.out

If you're looking for a process running a given executable, run fuser.

$ fuser /path/to/a.out
/path/to/a.out: 1234e 1239e
Related Question