Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I have the PID number for a process (on a UNIX machine), how can I find out the name of its associated process?

What do I have to do?

share|improve this question
5  
You can use ps or ls -l /proc/$PID/exe –  Eddy_Em Aug 17 '13 at 7:25
1  
@Eddy_Em that'll give you the executable file, which isn't always the process name. Also, that's not portable... –  derobert Aug 21 '13 at 21:44

3 Answers 3

On all POSIX-compliant systems, and with Linux, you can use ps:

ps -p 1337 -o comm=

Here, the process is selected by its PID with -p. The -o option specifies the output format, comm meaning the command name.

See also: ps – The Open Group Base Specifications Issue 6

share|improve this answer
1  
comm seems to truncate the command to 15 characters. Using command instead fixes it. –  Capt.Nemo Aug 15 '14 at 17:10

You can find the process name or the command used by the process-id or pid from

/proc/pid/cmdline

by doing

cat /proc/pid/cmdline

Here pid is the pid for which you want to find the name
For exmaple:

 # ps aux

   ................
   ................
   user  2480  0.0  1.2 119100 12728 pts/0  Sl   22:42   0:01 gnome-terminal
   ................
   ................

To find the process name used by pid 2480 you use can

# cat /proc/2480/cmdline 

 gnome-terminal
share|improve this answer
8  
Be careful: The OP mentions UNIX. Not all UNIXes implement the Plan 9 like process-specific file. Your answer generally only applies to Linux. –  slhck Aug 17 '13 at 8:08

You can Also use awk in combination with ps

ps aux | awk '$2 == PID number for a process  { print $0 }'

example:

root@cprogrammer:~# ps aux | awk '$2 == 1 { print $0 }'
root         1  0.0  0.2  24476  2436 ?        Ss   15:38   0:01 /sbin/init    

to print HEAD LINE you can use

 ps --headers aux |head -n 1 && ps aux | awk '$2 == 1 { print $0 }'

                 (or) 

 ps --headers aux |head -n 1; ps aux | awk '$2 == 1 { print $0 }'


root@cprogrammer:~# ps --headers aux |head -n 1 && ps aux | awk '$2 == 1 { print $0 }'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2  24476  2436 ?        Ss   15:38   0:01 /sbin/init
share|improve this answer
2  
This is unstable since it'd also select processes that happen to include the number anywhere in their command. Try ps ax | grep 1 and see whether it really returns the init process, for example. (In my case, it returns 119 lines—not desirable.) –  slhck Aug 17 '13 at 9:41
1  
@slhck Modified the answer... thanks for info.. ps -p 1 -o comm= is best option for this question. –  Gangadhar Aug 17 '13 at 11:07

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.