.pid Files in Linux: Purpose, Location, and Best Practices
21:52, 25.11.2026
Overview
“.pid” files in Linux are process identification, and they are needed for the storage of the process ID of functioning processes. A unique PID number is given to every process during its creation, and it is crucial for its identification in the OS. Such a file is usually called depending on the name of the process. Here in the article, we will talk more about such types of files and their mechanism of functioning.
Understanding PID Files: Purpose and Definition
PID files are regular text files that include the PID of a certain running process. This process acts as follows: during the start of the process, the file is created, and once the process ends, it is deleted. They are used so that sys scripts and other processes can interact with the running processes. Such files are inseparable when talking about the process monitoring and service management tasks.
With the usage of these files, system admins can check data regarding a specific process or even stop the process if needed.
How to Generate a .pid File
The generation of these files is fairly simple and can be done with the use of the following command:
echo $$ > myShell.pid
In this scenario, $$ is PID for the current script, and it is written to the mentioned file.
Another variant of the generation might be done with the utilization of a simple script, as follows:
#!/bin/bash
# PID file
pid_file="process.pid"
# create PID file
echo $$ > "$pid_file"
count=0
while [ $count -le 10 ]
do
echo "Going $count.."
sleep 1
((count++))
done
Where Are .pid Files Stored?
There is no specific rule for where the file should be stored. Nevertheless, the most standard location is considered to be /var/run. In order to exclude some misunderstanding with the other processes, it is possible to make a new directory, such as /var/run/myScript. If this directory is owned by root, it is possible to used home directory.
Terminating a Process via its PID File
Probably the main usage of the .pid file is connected with the termination of the running process. For the execution of this process, we actually require only the location and the actual name of the file.
kill $(cat process.pid)
With the usage of this command, the content is read from the file process .pid, and kill actually terminates the process.
Preventing Multiple Application Instances
Another usage of these files is to prevent multiple application instances. This process can be achieved with the usage of such scripts as:
#!/bin/bash
pid_file="process.pid"
# check if PID file exists
if [ -f "$pid_file" ]; then
pid=$(cat "$pid_file")
# check if process is running
if kill -0 "$pid" 2>/dev/null; then
echo "Process already running (PID $pid)"
exit 1
else
echo "Stale PID file found. Removing..."
rm -f "$pid_file"
fi
fi
# write current PID
echo $$ > "$pid_file"
# remove PID file when script exits
trap "rm -f $pid_file" EXIT
count=0
while [ $count -le 10 ]
do
echo "Going $count.."
sleep 1
((count++))
Done
When using such a script, we are clarifying that the process is actually running, deleting stale PID files, and use trap so that the file will be automatically deleted in case the script exists.
Summary
Now, you have a better understanding of the .pid files and how they really function in a Linux system. We shared all the basic commands related to the generation of the files, termination of the process, and prevention of multiple application instances. This information can be extremely helpful for the administration of Linux systems.