- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
What is a .pid File in Linux?
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.
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.
- Related Articles
- What is PID manager in Linux?
- Extracting a WAR File in Linux
- Creating a Temporary File in Linux
- Find the Process That is Using a File in Linux
- What does opening a file actually do on Linux?
- Append Lines to a File in Linux
- Advanced File Permissions in Linux
- Read Random Line From a File in Linux
- Remove Blank Lines From a File in Linux
- Count lines in a file using Linux bash
- Display specific columns of a file in Linux?
- Create a file of certain size in Linux
- Remove Lines Which Appear in File B From Another File A in Linux
- File globbing in Linux in C++
- How to sort a file in-place in Linux?