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 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.,, 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:


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


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.

Comments are closed.

Hire us!