If I know the PID number of a process, how can I get its name?
Solution 1
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.
For the full command, not just the name of the program, use:
ps -p 1337 -o command
See also: ps
– The Open Group Base Specifications Issue 6
Solution 2
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 example:
# 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
Solution 3
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]
Or also:
readlink /proc/[PID]/exe
Solution 4
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
Solution 5
# 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
Related videos on Youtube
AndreaNobili
Updated on September 18, 2022Comments
-
AndreaNobili about 1 year
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?
-
Eddy_Em about 10 yearsYou can use
ps
orls -l /proc/$PID/exe
-
Temak about 7 years
ps -fp PID
will show full command -
Pedro Lobito over 3 years
readlink /proc/$PID/exe
-
-
slhck about 10 yearsBe careful: The OP mentions UNIX. Not all UNIXes implement the Plan 9 like process-specific file. Your answer generally only applies to Linux.
-
slhck about 10 yearsThis 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 theinit
process, for example. (In my case, it returns 119 lines—not desirable.) -
Gangadhar about 10 years@slhck Modified the answer... thanks for info.. ps -p 1 -o comm= is best option for this question.
-
Hank about 9 yearscomm seems to truncate the command to 15 characters. Using
command
instead fixes it. -
Mike Lee over 7 years
ps -a
list all the processes that is associated with the terminal, it doesn't take any input. -
dave_thompson_085 over 7 yearsWe 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 over 7 yearsDoesn'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 thegrep
itself. All (AFAIK)ps
do have-p
, so justps -fp 33
. -
OmarOthman over 7 years[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 over 7 years[Ubuntu 14.04.4 LTS]
cat /proc/1/comm
=> init, not /sbin/init. His answer has the longer version included. But +1 anyway. -
jayarjo almost 7 yearsThis one is perfect.
-
Toby Speight almost 7 yearsApart from being highly inefficient compared to
ps -p${pid}
, this will pick up plenty of false positives - including thegrep
itself. -
robbie almost 7 yearsActually,
comm
gives the binary's name andcommand
returns argument 0 -
Andrew White over 6 yearsWhilst 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
-
Pedro Lobito over 6 years@MichaelLee I guess it depenends on the
ps
version, onprocps version 3.2.7
works fine. -
Pablo A over 5 yearsProbably better:
readlink /proc/1337/exe
. readlink - print resolved symbolic links or canonical file names. -
Daniel Andrei Mincă about 5 yearsThis 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 justjava
, but the rest of parameters passed will be displayed fully withpmap
. -
ttimasdf almost 4 yearsSome 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 almost 4 yearsHowever if I want the executable name, this
/proc
way is perfect. -
Admin over 1 yearsame answer was given 9 years ago !