What is the pid file? where is its location and what is its use?

What is a pid file? what is its location and where it is used ?
if i create a shell script which runs and do only sleep and its runs on background, will it create a pid file?
where will be located ?
jaisonshereenAsked:
Who is Participating?
 
Hanno P.S.Connect With a Mentor IT Consultant and Infrastructure ArchitectCommented:
Usually PID files are being stored eitehr under /var/.... somewhere or (more
historically) in /etc with a name like
  programname.pid

For example, if you have a background process with the name mybgproc,
you could create a file /var/tmp/mybgproc.pid which only contains the PID
of the proc just started.

This will allow for easy stopping of mybgproc with the command
  kill -9 `cat /var/tmp/mybgproc.pid
0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
A PID file will have to be created by the process (in your case your script) itself.

The purpose is to easily find the PID (Process ID) of the active process without
need to run a "ps -ef | grep <process-name>" or the like.
0
 
jaisonshereenAuthor Commented:
Where will be the file? And will it be deleted when the process stops?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
Also, when the process gets stopped (or even worse: dies) you
will have to make sure it gets cleared/removed yourself.
0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
oops, typo alert --> it should read
   
kill -9 `cat /var/tmp/mybgproc.pid`

Open in new window

0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:

#!/bin/sh
 
# a sample shell script to illustrate usage of PID file
 
# save this script's PID in a file
echo $$ > /var/tmp/myscript.pid
 
# make sure PID file will get removed when we get killed:
trap 'echo "I got killed!"; rm -f var/tmp/myscript.pid'  2 3
 
# this is a loop that runs forever
while : ; do sleep 60; done

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.