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).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
/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
Method 2
On a platform with a standard procfs with procfs-2 support, you get the result via:
ls -l /proc/<pid>/path/a.out
The file a.out is a symlink to the absolute path of the binary.
Note that the question did not mention Linux and for this reason, it makes sense to give information on how this feature works on the procfs original implementation from the procfs inventor Roger Faulkner.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0