pidof Command in Linux

Introduction

In this tutorial, you'll discover how to leverage the pidof command within Linux environments to pinpoint the process ID (PID) of actively running processes. The pidof utility is an essential tool for systemadmin tasks, including system monitoring and efficient system management. Gaining proficiency with pidof enables you to swiftly determine a process's PID, a critical piece of information when interaction with or termination of specific processes is required. This guide commences with an overview of the fundamental aspects of the pidof command, followed by practical exercises on locating PIDs of running processes and identifying multiple processes sharing the same name.

Understand the pidof Command

This section focuses on explaining the pidof command in Linux. Learn how it helps you discover the process ID (PID) of a running process.

The pidof command is a valuable asset for system monitoring and management. It significantly simplifies the task of identifying a process's PID, which proves particularly useful when intervention or termination of a process becomes necessary.

To effectively utilize the pidof command, simply input the name of the process you wish to identify. For example, to ascertain the PID of the nginx process, execute the following command:

pidof nginx

Example output:

1234 5678

The displayed output indicates that two processes named nginx are currently active, identified by their respective PIDs: 1234 and 5678.

Furthermore, the pidof command offers the capability to locate the PID of a process initiated via a specific command. For instance, to pinpoint the PID of the python3 process executing a script named my_script.py, execute the following command:

pidof -x my_script.py

Example output:

9012

The -x option instructs pidof to search for the PID of the process started by the exact command specified, instead of solely relying on the process name.

The subsequent section provides instructions on utilizing the pidof command to determine the PID of a currently running process.

Find the PID of a Running Process

This section guides you through the process of using the pidof command to identify the process ID (PID) of a process that is currently active.

To illustrate this, we'll initiate a new process. Open a terminal and run the command below to launch a straightforward Python script:

python3 -c "import time; print('Running process...'); time.sleep(60)"

This command initiates a Python process that will operate for 60 seconds, displaying "Running process..." in the terminal.

Next, in a separate terminal window, employ the pidof command to determine the PID of the running Python process:

pidof python3

Example output:

12345

The output reveals the PID of the active Python process, which in this instance is 12345.

You can validate the PID and process details using the ps command:

ps -p 12345 -o pid,comm

Example output:

  PID COMMAND
12345 python3

The ps command confirms that PID 12345 corresponds to the python3 process.

Equipped with the knowledge of how to ascertain the PID of a running process, you can now leverage this information to manage or terminate the process as necessary.

Locate Multiple Processes with the Same Name

This section demonstrates the use of the pidof command for locating several processes sharing an identical name.

As a starting point, launch two instances of the nginx web server:

sudo nginx
sudo nginx

Now, utilize the pidof command to discover the PIDs associated with the active nginx processes:

pidof nginx

Example output:

1234 5678

The output indicates the presence of two running nginx processes, identified by PIDs 1234 and 5678.

For detailed information about the running nginx processes, use the ps command:

ps -p 1234 -o pid,comm
ps -p 5678 -o pid,comm

Example output:

  PID COMMAND
 1234 nginx
 5678 nginx

The ps command verifies that both PIDs are linked to the nginx process.

To manage a specific nginx process, utilize its PID to interact with it. For instance, to terminate a specific nginx process, use the following command:

sudo kill 1234

This will terminate the nginx process with the PID 1234.

In conclusion, the pidof command is an invaluable tool for locating numerous processes sharing the same name, and these PIDs empower you to manage these processes individually.

Summary

This tutorial has provided a comprehensive overview of the pidof command within Linux, detailing its function in identifying the process ID (PID) of active processes. You've gained an understanding of how pidof streamlines system monitoring and management, allowing for rapid PID identification when interaction or termination of a specific process is required. Furthermore, you've learned to utilize the pidof command to discover the PID of a running process, including processes initiated by particular commands. Mastering pidof is a key skill for any systemadmin.

400+ Linux Commands