Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using putty to connect my linux server which my sites are in. I can run

# ps aux | less

to show running processes. But I want to display which files running these processes ?

I have also tried

# ps aux | grep <pid>

But getting this error.

enter image description here

What is the correct way to do it ?

share|improve this question
4  
in grep <pid>, you're supposed to replace <pid> with your actual process id (it's a number). <pid> is just a placeholder, not something you should actually type. –  Ben Voigt Apr 6 '13 at 14:37
add comment

migrated from stackoverflow.com Apr 6 '13 at 14:33

This question came from our site for professional and enthusiast programmers.

4 Answers

ps aux | grep <pid> isn't a shell command. The notation <pid> is a common convention to mean “put the PID there”. For example, if the PID is 1234, you'd run ps aux | grep 1234. This isn't a very useful command, you might as well run ps u 1234.

Your question isn't very clear. What do you mean by “with file names”? If you want to see what executable the process 1234 is running:

ls -l /proc/1234/exe

If you want to see what files that process has open:

ls -l /proc/1234/fd

If you want to see the full command line with arguments:

tr '\0' '\n' </proc/1234/cmdline

If you prefer to use commands, lsof -p1234 shows all the files the process has open. ps uww 1234 shows various pieces of information about process 1234 including the full command line.

share|improve this answer
add comment

try executing the following:

ps -ef
share|improve this answer
add comment

some characters under shell are for special purpose like < > ? " ; ...

grep PID is not useful because PID is process id and grep will find similar numeric ...

share|improve this answer
add comment

First of all, ps aux | grep <PID> is useful when you want to show the details for a specific process whose PID (Process Identifier) is represented by <PID>.

For example (ps aux | grep 'firefox\|USER' means print only lines that contein firefoxor USER):

ps aux | grep 'firefox\|USER'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
terdon  9021  2.5 11.8 1950888 970832 ?      Sl   Apr03 108:41 /opt/firefox/firefox

So, the PID for my firefoxinstance is 9021. So, to use the command you are trying to run, I would replace <PID> with 9021:

ps aux | grep 9021
terdon  9021  2.5 11.5 1948776 942640 ?      Sl   Apr03 109:03 /opt/firefox/firefox

Now, I am not sure what you mean by "display which files running these processes". If you mean display which files these processes are using, ps aux is one way of doing so. Say I have opened the file /usr/share/doc/nano/faq.html using this command:

 firefox /usr/share/doc/nano/faq.html 

I could see which file firefox had opened using ps :

ps aux | grep firefox
terdon   31763 18.7  1.0 682916 84352 pts/10   Sl+  17:10   0:02 firefox /usr/share/doc/nano/faq.html

The opened file is shown as one of the arguments passed to firefox. Note that the PID is different (it is now 31763 instead of 9021) that is because every running program has its own unique PID.


Another useful command is top. If you run it with the -c switch it will show the arguments passed to a command, and with -u <your user> it will only show processes started by your user name (replace <your user> with your actual user name):

top -c -u terdon

This is the output on my local machine (user terdon is only running two processes):

top - 17:14:41 up 3 days, 49 min, 14 users,  load average: 0.48, 0.54, 0.55
Tasks: 228 total,   1 running, 226 sleeping,   0 stopped,   1 zombie
%Cpu(s):  7.1 us,  5.5 sy,  0.0 ni, 86.5 id,  0.0 wa,  0.0 hi,  0.9 si,  0.0 st
KiB Mem:   8187940 total,  8007220 used,   180720 free,   349264 buffers
KiB Swap:  8191996 total,     5556 used,  8186440 free,  4173004 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                       
31573 terdon    20   0 24900 5576 1684 S   0.0  0.1   0:00.20 bash                                                          
31763 terdon    20   0  666m  77m  28m S   0.0  1.0   0:02.39 firefox /usr/share/doc/nano/faq.html                          

For an explanation of the information shown by top, see my answer to a related question on SU.

share|improve this answer
add comment

Your Answer

 
discard

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