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 ?
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

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
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.
jaisonshereenAuthor Commented:
Where will be the file? And will it be deleted when the process stops?
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

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

Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:

# 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

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.