If I know the PID number of a process, how can I get its name?

280

70

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?

AndreaNobili

Posted 2013-08-17T07:12:47.903

Reputation: 5 433

9ps -fp PID will show full command – Temak – 2016-11-04T00:10:28.247

9You can use ps or ls -l /proc/$PID/exe – Eddy_Em – 2013-08-17T07:25:48.117

1@Eddy_Em that'll give you the executable file, which isn't always the process name. Also, that's not portable... – derobert – 2013-08-21T21:44:32.283

Answers

308

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

slhck

Posted 2013-08-17T07:12:47.903

Reputation: 182 472

33comm seems to truncate the command to 15 characters. Using command instead fixes it. – Nemo – 2014-08-15T17:10:36.227

1[Ubuntu 14.04.4 LTS]

$ ps -p 1 -o comm=

init

$ ps -p 1 -o command=

/sbin/init; which means it is not about 15 characters, maybe just the binary's name vs. its full path. – OmarOthman – 2016-07-23T09:48:11.057

3Actually, comm gives the binary's name and command returns argument 0 – robbie – 2017-01-01T22:04:23.497

53

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

Stormvirux

Posted 2013-08-17T07:12:47.903

Reputation: 877

3Whilst that's true, they did tag the question "linux". Anyone who is using a non-Linux based UNIX OS will be quite used to having to modify answers to fit their needs – Andrew White – 2017-05-09T03:33:32.343

14Be careful: The OP mentions UNIX. Not all UNIXes implement the Plan 9 like process-specific file. Your answer generally only applies to Linux. – slhck – 2013-08-17T08:08:56.567

15

To get the path of of the program using a certain pid you can use:

ps ax|egrep "^ [PID]"

alternatively you can use:

ps -a [PID]

enter image description here

Pedro Lobito

Posted 2013-08-17T07:12:47.903

Reputation: 573

1ps -a list all the processes that is associated with the terminal, it doesn't take any input. – Michael Lee – 2016-03-10T18:30:49.923

@MichaelLee I guess it depenends on the ps version, on procps version 3.2.7 works fine. – Pedro Lobito – 2017-05-27T13:11:11.927

8

# ls -la /proc/ID_GOES_HERE/exe

Example:

# ls -la /proc/1374/exe
lrwxrwxrwx 1 chmm chmm 0 Mai  5 20:46 /proc/1374/exe -> /usr/bin/telegram-desktop

Carlos H. M. Moreira

Posted 2013-08-17T07:12:47.903

Reputation: 81

This one is perfect. – jayarjo – 2016-11-17T17:58:06.073

2Probably better: readlink /proc/1337/exe. readlink - print resolved symbolic links or canonical file names. – Pablo A – 2018-03-06T00:58:03.483

Some one commented on the question that the executable name is not always the process name, e.g. gunicorn or nginx. Some processes will change it at runtime with setprocname – ttimasdf – 2019-11-26T02:44:26.977

However if I want the executable name, this /proc way is perfect. – ttimasdf – 2019-11-26T02:45:14.540

8

You can use pmap. I am searching for PID 6649. And cutting off the extra process details.

$ pmap 6649 | head -1
6649:   /usr/lib64/firefox/firefox

Mike Studer

Posted 2013-08-17T07:12:47.903

Reputation: 81

This command helped me more than I needed, I have the full line of the process that started. Given a Java process, with the ps command all you'll see is just java, but the rest of parameters passed will be displayed fully with pmap. – Daniel Andrei Mincă – 2018-08-31T07:21:09.363

3

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

Gangadhar

Posted 2013-08-17T07:12:47.903

Reputation: 321

We don't need two runs to retain headers, instead use ps aux | awk 'NR==1 || $2==PID' -- and don't need to say {print $0} because it's the default. But as you commented, -p is better anyway. – dave_thompson_085 – 2016-05-06T04:38:21.787

2This 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 – 2013-08-17T09:41:08.920

1@slhck Modified the answer... thanks for info.. ps -p 1 -o comm= is best option for this question. – Gangadhar – 2013-08-17T11:07:58.463

3

Simmilar to slhck's Answer, but relying on file operations instead of command invocations:

MYPID=1
cat "/proc/$MYPID/comm"

ThorSummoner

Posted 2013-08-17T07:12:47.903

Reputation: 861

[Ubuntu 14.04.4 LTS] cat /proc/1/comm => init, not /sbin/init. His answer has the longer version included. But +1 anyway. – OmarOthman – 2016-07-23T22:17:41.693

2

Surprisingly, no one has mentioned the -f (full command) option for ps. I like to use it with -e (everything) and pipe the results to grep so I can narrow my search.

ps -ef | grep <PID>

This is also very useful for looking at full commands that someone is running that are taking a lot of resources on your system. This will show you the options and arguments passed to the command.

jdelaporte

Posted 2013-08-17T07:12:47.903

Reputation: 126

3Doesn't work on BSD (maybe including MacOSX? I'm not sure). Even where -e -f are available, grep can produce many false matches e.g. grep 33 includes pid=933 or 339, ppid=33 or 933 or 339, timeused of 33 seconds or 33 minutes, or programname or argument containing 33 -- including the grep itself. All (AFAIK) ps do have -p, so just ps -fp 33. – dave_thompson_085 – 2016-05-06T04:41:54.477

0

I find the easiest method to be with the following command:

ps -awxs | grep pid

user666282

Posted 2013-08-17T07:12:47.903

Reputation: 11

Apart from being highly inefficient compared to ps -p${pid}, this will pick up plenty of false positives - including the grep itself. – Toby Speight – 2016-11-22T16:55:14.207

0

made a simple script to find PID and use within bash scripts...
use with caution!!

Screenshot:
Screenshot

http://pastebin.com/Cm9YH67U

André

André Verwijs

Posted 2013-08-17T07:12:47.903

Reputation: 151