248

I recently came across a file with the extension .pid and looked inside it but didn't find much. The documentation says:

A Pid-File is a file containing the process identification number (pid) that is stored in a well-defined location of the filesystem thus allowing other programs to find out the pid of a running script.

Can anyone shed more light on this or guide me to details of what's contained in the pid file?

4 Answers 4

265

The pid files contains the process id (a number) of a given program. For example, Apache HTTPD may write its main process number to a pid file - which is a regular text file, nothing more than that - and later use the information there contained to stop itself. You can also use that information to kill the process yourself, using cat filename.pid | xargs kill

10
  • 13
    Why not look up the process by name then? Why bother with maintaining .pid files when you can just run "pidof $process_name" and get the ID?
    – Shnatsel
    Commented Jun 4, 2013 at 15:23
  • 32
    @Shnatsel: because there might be two processes with that name running, and you need to know which one is in charge of that PID file. There are other reasons, more details are found here: unix.stackexchange.com/questions/12815/… Commented Aug 27, 2013 at 13:40
  • 5
    In that case there would be 2 pid files and you's be facing the same issue as with PID lookups. So pidfiles do not to any good and only complicate things in this scenario as well as any other scenario I can think of. I suspect they either appeared before procfs did or they're used as portability tool because procfs interfaces are different on e.g. Solaris are quite different from that on Linux.
    – Shnatsel
    Commented Aug 28, 2013 at 15:05
  • 3
    Just one thing: echo filename.pid | xargs kill will not kill the process. I guess you meant: echo $(cat filename.pid) | xargs kill or echo <process ID> | xargs kill. Commented Apr 28, 2017 at 22:04
  • 4
    @AlexandrodeOliveira Is there an advantage of using echo and cat instead of just cat filename.pid | xargs kill? Commented Dec 8, 2017 at 11:33
1

Not sure if that's these are the only reasons, but here's my the drill:

Depending on the way you write a shellscript to kill the desired proccess you could end up killing the kill PID before it kills your target, let's take mydaemon for example:

kill -9 `ps ax | grep mydaemon | awk '{ print $1 }'`

A) SIGPIPE-ing kill In a 32-bit Linux PID is usually a 15-bit integer, overflows do happen often, there's a fairly big chance that the grep or awk PIDs will appear prior to mydaemon's one. In 64-bit PID numbers are usually 22-bit, it's more than 100x less likely to happen, yet still pretty factible.

By killing either one of your pipes you'll receive a SIGPIPE and usually this means death as well, therefore kill would be killed before killing mydaemon rendering the kill attempt a fail.

B) Killing other PIDs Also, say you had vi /etc/mydaemon/mydaemon.conf running altogether, that PID might also be killed, not to mention other users' processes since you much likely would be issuing such command as root.

C) It's a simple unix-like lock -> No additional code/daemon required. PidFiles make a fairly simple way to create user-manageable locks to keep you from spawning a daemon twice inadvertently.

0

Pidfile contains pid of a process. It is a convention allowing long running processes to be more self-aware. Server process can inspect it to stop itself, or have heuristic that its other instance is already running. Pidfiles can also be used to conventiently kill risk manually, e.g. pkill -F <some.pid>

-3

To understand pid files, refer this DOC

Some times there are certain applications that require additional support of extra plugins and utilities. So it keeps track of these utilities and plugin process running ids using this pid file for reference.

That is why whenever you restart an application all necessary plugins and dependant apps must be restarted since the pid file will become stale.

2
  • 8
    Your first link does not really answer the question. Commented Jul 1, 2014 at 16:57
  • 5
    Whilst the linked material may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Commented Jan 15, 2018 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.