How to remove orphaned PID files

Install an configure tacacs+ on Debian 8 – part 2
February 13, 2017
Linux SSH authentication with Google
December 22, 2017
Show all

How to remove orphaned PID files

According to wikipedia.com a PID (proces identifier) is a number used by most operating system kernels — such as that of UNIX, macOS or Microsoft Windows — to uniquely identify an active process. This number may be used as a parameter in various function calls, allowing processes to be manipulated, such as adjusting the process’s priority or killing it altogether.

Some processes, for example, the moc music player and the MySQL daemon, write their PID to a documented file location, to allow other processes to look it up.

What to do in case, for some strange reason, that pid file never gets deleted when the process stops?

Well, 1st of all, you need to find out why that happens. However, this is not the reason I write this now.

In case the PID files are not deleted, although highly unlikely, it may happen that your hard drive will run out of space, so you need to find a way to remove those orphaned PID files.

A simple visual inspection of the directory containing the PID files ( e.g.  ls -ln /var/run/*.pid ) will show you all the PID files in /var/run/ directory.

If you have multiple PID files for the same process ( e.g. 1_process_name.pid, 2_process_name.pid, ecc. ) you may have a situation like the one described above.

So, how do you delete the PID files for the processes that are no longer running, without interfering with the running processes?

Check the below script:

#
# SCRIPT : delete_orphaned_PID_files.sh
# AUTHOR : Marin Nedea 
# WEBSITE : http:#sysadmins.tech 
# BLOG : http:#sysadmins.tech 
# CREATED : 31/07/2017 
# COMMENT : Script to delete orphaned PID files.
# LICENSE : Copyright (C) 2016 - Marin Nedea @ http:#sysadmins.tech
# 
#           This program is free software: you can redistribute it and/or modify
#           it under the terms of the GNU General Public License as published by
#           the Free Software Foundation, either version 3 of the License, or
#           at your option) any later version.
# 
#           This program is distributed in the hope that it will be useful,
#           but WITHOUT ANY WARRANTY; without even the implied warranty of
#           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#           GNU General Public License for more details.
# 
#           You should have received a copy of the GNU General Public License
#           along with this program. If not, see <http:#www.gnu.org/licenses/>.
#
# HOWTO   : Save the script to a location of your choice, chmod +x delete_orphaned_PID_files.sh,
#           then run as root ./delete_orphaned_PID_files.sh 
#			Don't forget to replace "example_process" with the actual service/process
 
#!/bin/bash

#Define the process name
process_name=example_process
processpid=$(ps axf | grep $process_name | grep -v grep | awk '{print $1}')  	        #get the running $process_name PID
dir=/var/run/*$process_name.pid                                     	   		#get all $process_name.pid files in /var/run/ directory
for f in $dir                                                           	   	#for each file in $dir
do
    pid=$(< "$f")                                                       	   	#assign to var $pid the value in the file
      if [ "$processpid" -eq "$pid" ]
            then 
                  echo "$process_name is running on PID $pid. Keeping $f intact."
            else 
                  echo "No process running on PID $pid. Deleting the $f file!"
                  rm -f $f                                               	   	#deleting the file corresponding to the value of $f
      fi
done

 

Marin Nedea
Marin Nedea
I'm passionate about open source software and technologies. In my spare time I build simple and functional websites from scratch, using PHP+HTML5+CSS3+MySQL and when I'm bored, I write simple PHP_CLI or bash scripts to play around on my Linux machine.

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close