Skip to content

Get a Process Name From a PID in Linux, or a PID From a Name

Updated 5 min read

Updated: 12 October 2026 · Applies to: procps-ng 4.0.4 (ps, pgrep) and pidof on Debian 13, Ubuntu 24.04, Ubuntu 26.04 and AlmaLinux 10, tested

To get the process name from a PID, run ps -p PID -o comm=. It prints only the name, for example sleep. Use ps -p PID -o args= when you need the full command line with its path and options. To go the other way and get the PID from a process name, run pgrep -x name or pidof name.

ps -p 723 -o comm=      # name of PID 723
ps -p 723 -o args=      # full command line of PID 723
pgrep -x nginx          # PIDs of every process called nginx
pidof nginx             # the same, on one line

The output below is a lab example from a Debian 13 test container. Your PIDs will be different. ps and pgrep behaved the same on Ubuntu 24.04, Ubuntu 26.04 and AlmaLinux 10.

1. Get the process name from a PID

  1. Print the name only (the equals sign removes the column header, which is handy in scripts):
    $ ps -p 723 -o comm=
    sleep
  2. Show the name together with the owner, running time and full command line:
    $ ps -p 723 -o pid,user,etime,comm,args
        PID USER         ELAPSED COMMAND         COMMAND
        723 root           00:01 sleep           sleep 1000
  3. Read the same details straight from /proc, which works even on minimal systems without the ps tool:
    $ cat /proc/723/comm
    sleep
    $ readlink /proc/723/exe
    /usr/bin/sleep
    $ tr '\0' ' ' < /proc/723/cmdline; echo
    sleep 1000

comm is the short process name, exe is the program file that is running, and cmdline is the command with its arguments. Any user can read comm and cmdline, but readlink /proc/PID/exe on another user's process needs root: as a normal user it printed nothing and returned exit code 1 in our test.

If the PID does not exist, ps -p prints nothing and returns exit code 1, so a script can test it:

if ps -p "$PID" > /dev/null; then echo "running"; else echo "not running"; fi

2. Names are cut to 15 characters

The kernel stores only the first 15 characters of a process name. A program called backup-database-nightly shows up shortened:

$ ps -p 722 -o comm=
backup-database
$ ps -p 722 -o args=
/usr/local/bin/backup-database-nightly 1000

This also affects searches by name. pgrep warns you and finds nothing, so search the full command line with -f, or use pidof, which matched the full name in our test:

$ pgrep backup-database-nightly
pgrep: pattern that searches for process name longer than 15 characters will result in zero matches
Try `pgrep -f' option to match against the complete command line.
$ pgrep -f backup-database-nightly
722
$ pidof backup-database-nightly
722

3. Get the PID from a process name

pgrep prints one PID per line. Add -x for an exact name match (without it, pgrep sleep would also match a process called sleepy), -l to show the name, and -a to show the full command line:

$ pgrep -x sleep
2
723
727
735
$ pgrep -a sleep
2 sleep infinity
723 sleep 1000
727 sleep 900
735 sleep 2000

More pgrep options that answer common questions:

  • pgrep -n sleep: only the newest matching process (here 735); pgrep -o sleep: only the oldest.
  • pgrep -u alice sleep: only processes owned by the user alice.
  • pgrep -f "php artisan queue": match anywhere in the command line, useful for scripts and interpreters (PHP, Python, Java) where the process name is just php or java.

pidof prints all matches on one line (newest first in our test), and -s returns a single PID:

$ pidof sleep
735 727 723 2
$ pidof -s sleep
735

For shell scripts, pidof needs -x, because the running program is bash, not the script file:

$ pidof sync-job.sh
$ pidof -x sync-job.sh
724
$ pgrep -a sync-job
724 /bin/bash /usr/local/bin/sync-job.sh

Both commands print nothing and return exit code 1 when no process matches. On Debian and Ubuntu, pidof comes from the sysvinit-utils package; on AlmaLinux 10 it is part of procps-ng. pidof -x found the script with both versions.

For a service started by systemd, ask systemd for its main PID. According to the systemctl manual, systemctl show -P MainPID nginx prints only the value, and systemctl status 723 shows which service a PID belongs to.

4. List process IDs on Linux

To see every PID with its name, use one of these:

  • ps -e -o pid,comm: all PIDs and names, nothing else.
  • ps -ef: PID, parent PID (PPID), user, start time and full command.
  • ps aux: PID with CPU and memory use.
  • ps -u alice -o pid,comm: only the processes of one user.
  • pstree -p: processes as a tree with the PID in brackets (package psmisc).
$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 17:35 ?        00:00:00 /run/podman-init -- sleep infinity
root         437       1  0 17:36 ?        00:00:00 cron
root         722     710  0 17:37 ?        00:00:00 /usr/local/bin/backup-database-nightly 1000
root         723     710  0 17:37 ?        00:00:00 sleep 1000
root         724     710  0 17:37 ?        00:00:00 /bin/bash /usr/local/bin/sync-job.sh
root         727     724  0 17:37 ?        00:00:00 sleep 900
alice        735       1  0 17:37 ?        00:00:00 sleep 2000
(output shortened)

On a normal server PID 1 is systemd; here it is the init process of the test container. To sort by memory or CPU, add --sort, for example ps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head. For live views with top and htop, see How to List Running Processes in Linux (ps, top, htop and pgrep).

What to do with the PID next

A PID is only unique while the process runs. After it exits, Linux can give the same number to a new process, so look the name up again just before you act on an old PID.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket