1. Introduction

On Linux, the Process ID (PID) is a unique identifier assigned to each running process when it’s created. A .pid file is a process identification file that stores the process ID (PID) of running processes. We can use the .pid file to view details about a specific process or to terminate it.

Most processes store their .pid file in the /var/run directory or the /var/run/<process_name> directory. However, the exact location of the .pid file could be in a different directory depending on how the process is configured. When dealing with applications that use .pid files, we need to check the application’s configuration or documentation to determine the exact location of its .pid file.

In this tutorial, we’ll learn different methods and tools that we can use to find the .pid file of a specific process.

2. Searching Common Directories

We can find the .pid file of a specific process by searching through common directories such as /var/run or /var/run/<process_name> where processes store their .pid files.

For example, let’s use the ls command to view all the .pid files available in the /var/run directory:

$ ls /var/run/*.pid
/var/run/acpid.pid  /var/run/crond.pid  /var/run/docker.pid  /var/run/gdm3.pid  /var/run/nginx.pid

Alternatively, we can check within the /var/run/<process_name> directory.

Let’s start with listing the contents of the /var/run directory to list the directories of some of the running processes:

$ ls /var/run/
acpid.pid     avahi-daemon   crond.reboot  docker.pid   gdm3.pid    log     network         openvpn-client  sendsigs.omit.d    speech-dispatcher  tmpfiles.d        unattended-upgrades.lock   wpa_supplicant
acpid.socket  blkid          cups          docker.sock  initctl     motd.d  NetworkManager  openvpn-server  shm                spice-vdagentd     ubuntu-advantage  user                       xtables.lock

Then we can list the contents of a specific process directory to view the .pid file:

$ ls /var/run/redis

A different approach we can take is to use the find command to search for all .pid files within the /var/run directory and its subdirectories:

$ sudo find /var/run/ -name "*.pid"

We’re using the -name option to search for all files that contain the .pid extension.

This method is advantageous when we don’t know the exact name of the process we’re looking for or when we want to find .pid files within subdirectories.

3. Searching the Root Directory

We can also search through the root directory to find the .pid file of a specific process. This method might take a long time if we have a large storage space or a lot of files.

However, this process is more thorough since we can find .pid files for processes that store them in different locations.

To achieve this, we can use the find command to search the root directory. We need to run the command with root privileges:

$ sudo find / -name "*.pid"
[sudo] password for redward: 

This lists all the .pid files in the root directory and subdirectories.

3.1. Using the locate Command

Alternatively, we can use the locate command to search for all files with a .pid extension. To achieve this, we first need to install the mlocate package.

On Ubuntu, we can use the APT package manager:

$ sudo apt install mlocate

Alternatively, on Arch Linux, we can use Pacman:

$ sudo pacman -S mlocate

Finally, on Fedora, we can employ DNF:

$ sudo dnf install mlocate

After installation, we need to update the database with the updatedb command:

$ sudo updatedb

Then use the locate command to find all files with a .pid extension:

$ locate "*.pid"

3.2. Using fdfind Command

We can also use the Linux fdfind command to search for .pid files on the system. It’s a Linux tool that uses regular expressions to search for files.

The fdfind utility is available in almost all package repositories. We can install it on Debian from the APT package manager:

$ sudo apt install fd-find

On Arch Linux, we can use Pacman:

$ sudo pacman -S fd-find

Finally, we can use DNF for Fedora:

$ sudo dnf install fd-find

We can now use the fd-find command to search for all files with a pid extension in the root directory:

$ fdfind . '/' --hidden -e pid

We’re using the –hidden option to include hidden files and directories in our search. By default, the fdfind command ignores hidden files and directories. The -e option specifies the file extension that we’re matching.

The command also outputs the path of each .pid file found, making it easy to locate and manipulate.

4. Conclusion

In this article, we’ve explored the different approaches and tools we can use when locating the .pid file of a given process.

The first method covers searching for the .pid file in the most common directories while using tools such as the ls command and the find command.

In the second method, we search the root directory and its subdirectories to find all available .pid files. This approach is handy when we’re unsure about the exact process name, or we can’t find it in the common directories.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.