How can I find out what the program file name is for a running process?
6 Answers
The following run from a shell will give you the command, its full path, and its invocation arguments all in the last column for all running programs:
ps -eF
This is the unix syntax, since you were not specific. There is also GNU and BSD syntaxes available in linux. man ps
to learn more.
Note that all of the above commands will only work some of the time. For example, here the output of "ps" shows the path to a program, but if you try to access that path you find that nothing is there:
$ ./myprogram &
$ rm myprogram
$ ps -fe | grep myprogram
lars 27294 29529 0 20:39 pts/1 00:00:00 ./myprogram
$ ls myprogram
ls: myprogram: No such file or directory
In fact, the value displayed by ps is entirely up to the whatever code launched the program. For example:
$ python -c "import os; os.execl('./myprogram', '/usr/sbin/sendmail')" &
myprogram: i am: 27914
$ ps -f -p 27914
UID PID PPID C STIME TTY TIME CMD
lars 27914 29529 0 20:44 pts/1 00:00:00 /usr/sbin/sendmail
So basically, you can't rely on the output of ps. You might be able to rely on /proc/PID/exe
, for example:
$ ls -l /proc/27914/exe
lrwxrwxrwx 1 lars lars 0 Dec 3 20:46 /proc/27914/exe -> /home/lars/tmp/myprogram
But even in this case the file may no longer exist.
-
larsks: +1 Nice information! Welcome to serverfault, hope to see more of your answers! Dec 4, 2009 at 2:14
How about something like this.
lsof -p pid | grep 'txt'
man lsof
-p s This option excludes or selects the listing of files for
the processes whose optional process IDentification (PID)
numbers are in the comma-separated set
...
FD is the File Descriptor number of the file or:
txt program text (code and data);
if your programs reside deeply in a /usr/local/program/bin filesystem tree it is possible on a limited terminal not to see the full path and program.
you can use:
ps -auxww
to see the full unlimited command line which belongs to all the processes.
Out of the ps manpage:
w Wide output. Use this option twice for unlimited width.
On Linux it's very simple:
$ cat /proc/4670/cmdline
kdeinit4: plasma-desktop [kdeinit]
Don't parse anything until you're sure there's no other way.
Cheers! :)