9 Simple ways to find the Process ID (PID) of a Program Running on Linux

Everyone knows about PID, Do you have below questions in your mind

  • What PID does exactly?
  • Why do you need a PID?
  • What are we going to do using PID?

If so, you are in the right place to get all the details.

Essentially, we are looking for a PID to kill an unresponsive program, and it’s like the Windows Task Manager.The Linux GUI also provides the same feature, but the CLI is an efficient way to perform the kill function.

What is Process ID (PID)

PID refers to process ID, which is commonly used by most operating system kernels, such as Linux, Unix, MacOS and Windows.

This is a unique ID that is automatically assigned to each process when it is created. A process is a running instance of a program.

What is Parent Process ID (PPID)

A parent process is a process that has created one or more child processes. Each child process is given a Parental Process ID (PPID), and the parent process kills the child when it completes their operation.

You may be interested to read the below articles, as these are related to this topic.

 

 

 

Each time the process ID is changed to all processes except init. the init is always the first process in the system and the ancestor of all other processes. It holds PID 1.

The default maximum value of PIDs is 32,768. This can be verified by running the following command

cat /proc/sys/kernel/pid_max  on your computer.

32,768 is the maximum value on 32-bit systems, but can be set to any value up to 2^22 (approximately 4 million) on 64-bit systems.

You may ask, why do we need such size PIDs? This is because PIds cannot be reused immediately and to prevent potential errors.

You can find the PID of processes running on the system using the below nine methods, such as pidof command, pgrep command, ps command, pstree command, ss command, netstat command, lsof command, fuser command, and systemctl command.

This can be achieved using the below nine methods.

  • pidof: pidof – find the process ID of a running program.
  • pgrep: pgre – look up or signal processes based on name and other attributes.
  • ps: ps – report a snapshot of the current processes.
  • pstree: pstree – display a tree of processes.
  • ss: ss is used to dump socket statistics.
  • netstat: netstat is displays a list of open sockets.
  • lsof: lsof – list open files.
  • fuser: fuser – list process IDs of all processes that have one or more files open
  • systemctl: systemctl – Control the systemd system and service manager

To prove this, we are going to find the Apache process ID. Make sure to enter your process name instead of us.

Method-1: How to find the Process ID (PID) of a program running on Linux using the pidof Command

The pidof command is used to find the process ID of the running program. It prints those IDs into the standard output. To demonstrate this, we are going to find the Apache2 process id from Debian 9 (stretch) system.

# pidof apache2

3754 2594 2365 2364 2363 2362 2361

In the above output you may have difficulties identifying the process ID because it displays all PIDs (including parent and child) against the process name.

So we need to find the Parent Process PID (PPID), which is what we are looking for. This may be the first number. In my case it is 3754 and it is sorted in descending order.

Method-2: How to find the Process ID (PID) of a program running on Linux using the pgrep Command

The pgrep command looks at the processes currently running and lists the process IDs that match the selection criteria.

# pgrep apache2

2361
2362
2363
2364
2365
2594
3754

This is similar to the output above, but it sorted the results in ascending order, which clearly states that the parent process PID is last. In my case it is 3754.

Note: If you have more than one process ID, you may have trouble identifying the parent process ID when using the pidof & pgrep command.

Method-3: How to find the Process ID (PID) of a program running on Linux using the pstree Command

The pstree command shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified in the pstree command then it’s shows all the process owned by the corresponding user.

pstree visually merges identical branches by putting them in square brackets and prefixing them with the repetition count.

# pstree -p | grep "apache2"

           |-apache2(3754)-+-apache2(2361)
           |               |-apache2(2362)
           |               |-apache2(2363)
           |               |-apache2(2364)
           |               |-apache2(2365)
           |               `-apache2(2594)

To get only the parent process, use the following format.

# pstree -p | grep "apache2" | head -1

           |-apache2(3754)-+-apache2(2361)

The pstree command is very simple because it separates the parent and child processes separately, but is not easy when using the pidof & pgrep command.

Method-4: How to find the Process ID (PID) of a program running on Linux using the ps Command

The ps command displays information about a selection of the active processes. It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.

# ps aux | grep "apache2"

www-data  2361  0.0  0.4 302652  9732 ?        S    06:25   0:00 /usr/sbin/apache2 -k start
www-data  2362  0.0  0.4 302652  9732 ?        S    06:25   0:00 /usr/sbin/apache2 -k start
www-data  2363  0.0  0.4 302652  9732 ?        S    06:25   0:00 /usr/sbin/apache2 -k start
www-data  2364  0.0  0.4 302652  9732 ?        S    06:25   0:00 /usr/sbin/apache2 -k start
www-data  2365  0.0  0.4 302652  8400 ?        S    06:25   0:00 /usr/sbin/apache2 -k start
www-data  2594  0.0  0.4 302652  8400 ?        S    06:55   0:00 /usr/sbin/apache2 -k start
root      3754  0.0  1.4 302580 29324 ?        Ss   Dec11   0:23 /usr/sbin/apache2 -k start
root      5648  0.0  0.0  12784   940 pts/0    S+   21:32   0:00 grep apache2

From the above output you can easily identify the Parent Process ID (PPID) based on the process start date. In my case the Apache 2 process started on Dec11, which is the parent process and the others are the child process. Apache2’s PID is 3754.

Method-5: How to find the Process ID (PID) of a program running on Linux using the ss Command

The ss command is used to dump socket statistics. It allows showing information similar to netstat. It can display more TCP and state information than other tools.

It can display stats for all kind of sockets such as PACKET, TCP, UDP, DCCP, RAW, Unix domain, etc.

# ss -tnlp | grep apache2

LISTEN     0      128     :::80     :::*      users:(("apache2",pid=3319,fd=4),("apache2",pid=3318,fd=4),("apache2",pid=3317,fd=4))

Method-6: How to find the Process ID (PID) of a program running on Linux using the netstat Command

The netstat command is used to print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. By default, netstat displays a list of open sockets.

If you don’t specify any address families, then the active sockets of all configured address families will be printed. This program is obsolete. Replacement for netstat is ss.

# netstat -tnlp | grep apache2

tcp6       0      0 :::80      :::*           LISTEN      3317/apache2

Method-7: How to find the Process ID (PID) of a program running on Linux using the lsof Command

The lsof command is used to list open files. The Linux lsof command lists information about files that are open by processes running on the system.

# lsof -i -P | grep apache2

apache2   3317             root    4u  IPv6  40518      0t0  TCP *:80 (LISTEN)
apache2   3318         www-data    4u  IPv6  40518      0t0  TCP *:80 (LISTEN)
apache2   3319         www-data    4u  IPv6  40518      0t0  TCP *:80 (LISTEN)

Method-8: How to find the Process ID (PID) of a program running on Linux using the fuser Command

The fuser utility shall write to standard output the process IDs of processes running on the local system that have one or more named files open.

# fuser -v 80/tcp

                     USER        PID ACCESS COMMAND
80/tcp:              root       3317 F.... apache2 
                     www-data   3318 F.... apache2
                     www-data   3319 F.... apache2

Method-9: How to find the Process ID (PID) of a program running on Linux using the systemctl Command

The systemctl command is used to control the systemd service manager. This is a replacement for the old SysVinit system management, and most of the modern Linux operating systems have been moved to the systemd.

# systemctl status apache2

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Tue 2018-09-25 10:03:28 IST; 3s ago
  Process: 3294 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 3317 (apache2)
    Tasks: 55 (limit: 4915)
   Memory: 7.9M
      CPU: 71ms
   CGroup: /system.slice/apache2.service
           ├─3317 /usr/sbin/apache2 -k start
           ├─3318 /usr/sbin/apache2 -k start
           └─3319 /usr/sbin/apache2 -k start

Sep 25 10:03:28 ubuntu systemd[1]: Starting The Apache HTTP Server...
Sep 25 10:03:28 ubuntu systemd[1]: Started The Apache HTTP Server.

References:

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...