Show running processes with file name(s)?

118,317

Solution 1

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.

Solution 2

try executing the following:

ps -ef

Solution 3

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:
118,317

Related videos on Youtube

tshepang
Author by

tshepang

I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.

Updated on September 18, 2022

Comments

  • tshepang
    tshepang over 1 year

    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:

    -bash: syntax error near unexpected token `newline'
    

    What is the correct way to do it?

    • Admin
      Admin about 11 years
      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.