What is a .pid File in Linux?


Introduction

On Linux, a “.pid” file is a process identification (PID) file. It is used to store the process ID (PID) of a running process. The PID is a unique number assigned to each process when it is created and is used to identify the process in the operating system. The .pid file is usually located in the /var/run or /var/run/<name> directory and is named after the process it represents. In this article, we will discuss what .pid files are, how they are used, and how to work with them.

What is a PID file?

A PID file is a simple text file that contains the PID of a running process. The file is created when the process starts and is deleted after the process ends. System administrators, system scripts and other processes use the PID file to identify and interact with the running process. These files are especially useful when it comes to service management, process monitoring, and signals.

For example, a service script can use the PID file to determine if a service is running or stop the service by sending a signal to the process. A system administrator can use the PID file to view information about the process or to terminate the process. This is done using commands such as “pgrep” and “kill”, which we will discuss in detail later in this article.

Creating a .pid file

Creating a .pid file is a simple process and can be done with a simple command. One way to create a “.pid” file in a script is to pipe the output of “$$” to a file (in Bash shell) −

$ echo $$ > myShell.pid

$$ is a Linux variable that returns the PID of the calling process. In this case, it's the PID of the shell.

Another way to create a .pid file is to use a simple script like the following −

#!/bin/bash
  
# create file
pid_file="process.pid"
echo $$ > $pid_file

count=0
while [ $count -le 10 ]
do
  echo Going $count..
  sleep 1
  count=$(($count+1))
done

When this script is run it will spawn the process and create the .pid file containing the process ID.

Location of the .pid file

When it comes to the location of “.pid” files, there is no specific rule as to where they should be stored. However, there are some commonly used locations for these files. Typically, our process places files in /var/run. To avoid conflicts with other processes, we could go one step further and create a new directory, /var/run/myScript. However, some systems may have this directory owned by root, in which case it may not be possible to write the .pid file there. A second option would be the home (/home/user) directory.

Kill a process using a .pid file

One of the primary uses of “.pid” files is to kill a process while it's running. If there is a .pid file, we can get the PID of the file and then use it with xargs and kill. This ensures that we only need to know the name and location of the “.pid” file and not the PID itself.

$ cat process.pid | xargs kill

This command will take the contents of the .pid file, which is the process ID, and pass it as an argument to the kill command. This ensures that we only stop the exact process we want, instead of having to manually search for the process.

Guarantee a single instance of an application

Another use for .pid files is to ensure that only a single instance of an application is running. To do this, we need to remove the .pid file at the end of our run and add a check at the beginning to see if a .pid file exists. This can be done using the following script −

#!/bin/bash

pid_file="process.pid"

if [ ! -f $pid_file ]; then
  echo $$ > $pid_file
  count=0
  while [ $count -le 10 ]
   do
      echo Going $count..
      sleep 1
      count=$(($count+1))
   done
  rm $pid_file
else
  echo "Process already running"
fi

In this script, we first check if the “.pid” file exists. If it doesn't exist, we proceed to create the file and run the script. Once the script has finished running, the .pid file is deleted. However, if the .pid file already exists, the script is already running, so the message "The process is already running" is displayed and the script does not run.

This is a simple yet effective way to ensure that only one instance of the script is running at any given time.

Conclusion

In this article, we discuss what .pid files are and how they are used in Linux. We cover creating and locating “.pid” files, as well as tasks that can be performed with .pid files, such as killing a process and ensuring a single instance of an application. .pid files are a convenient way to track running processes and allow system administrators, scripts, and other processes to easily identify and interact with running processes. Understanding how to use .pid files can greatly simplify the process of administering and maintaining Linux systems.

Updated on: 20-Jan-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements