Linux – Get Full Executable Name of Running Process

filenamesprocprocess

I'm writing a script which will use the filename of running processes. However, I'm unable to determine the full executable name of some processes.

Initially, I decided to query the Name entry in /proc/PID/status (or the second field in /proc/PID/stat). However, according to the manpage, that field is always truncated to 15 characters, but I need the full name to avoid conflict/confusion.

An answer of this question suggests to use /proc/PID/cmdline, but there are problems too. Some programs (e.g. chromium, electron) do stupid/smart things to the value in /proc/PID/cmdline so I can't just split the data there by NULL and directly get the program name as suggested in the manpage – they fill in a lot of things to the original argv[0] field and separate them by space, and I don't think merely splitting by space is a good choice because the path/filename may contain spaces.

This is even more complicated when I find out that some scripts (e.g. python scripts) are in the form /usr/bin/python /path/to/script while some are in the form /path/to/script. Though this is much easier to deal with as long as I have that field (without jams as above) and manually check and split.

Any ideas how to get the full program name/filename?
It doesn't matter if the name contains the full path or not because that can be easily dealt with (as far as I can see now).

Best Answer

/proc/$PID/exe seems to be what you're looking for: (proc(5)

/proc/[pid]/exe
Under Linux 2.2 and later, this file is a symbolic link containing the actual pathname of the executed command. This symbolic link can be dereferenced normally; attempting to open it will open the executable.

So, simply:

$ /bin/cat & readlink /proc/$!/exe
/bin/cat

It actually follows renames on the executable file:

/tmp$ cp /bin/cat . ; ./cat & mv cat dog ; readlink /proc/$!/exe
/tmp/dog