Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I find out what the program file name is for a running process?

share|improve this question
2  
From where? A shell prompt? What shell? Within the program itself? What language? –  Dennis Williamson Dec 3 '09 at 22:22

6 Answers 6

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
larsks: +1 Nice information! Welcome to serverfault, hope to see more of your answers! –  Kyle Brandt Dec 4 '09 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);
share|improve this answer

In general, if you're looking for that from the shell, you can use ps.

share|improve this answer

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.
share|improve this answer

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! :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.