235

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?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
ifixthat
  • 6,137
  • 5
  • 25
  • 42

4 Answers4

252

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

Community
  • 1
  • 1
Rafael Steil
  • 4,538
  • 4
  • 25
  • 30
  • 1
    thanks lot got he point cause i also found same example son my system. containing only ids of the prcoess, so it can be application specific so i can also basically use it for my personal use under linux env right ?? – ifixthat Nov 28 '11 at 13:11
  • Yes, that is correct. It is application specific, only for that machine. Not all applications store pid files, but it is very common to find it across the system. – Rafael Steil Nov 28 '11 at 13:32
  • 10
    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 Jun 04 '13 at 15:23
  • 27
    @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: http://unix.stackexchange.com/questions/12815/what-are-pid-and-lock-files-for – user4815162342 Aug 27 '13 at 13:40
  • 4
    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 Aug 28 '13 at 15:05
  • 2
    @Shnatsel The are used primarily for the daemon itself. When running something like `daemonname start /for/this/path` it can check if a pidfile already exists for that location. Yes, you could check the process table, but the location of the pid file itself can add more information (for instance if the daemon is watching a particular folder). Also, the simplest mechanisms are often the most robust. Lastly, if the pidfile exists, but the process by that ID does not, it may indicate something else, such as other resources not being properly cleaned up. – eestrada Sep 02 '15 at 19:36
  • @Shnatsel Pid files are not just linux, all unix systems have them. – skywalker Nov 22 '15 at 11:44
  • 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 | xargs kill`. – Alexandro de Oliveira Apr 28 '17 at 22:04
  • 4
    @AlexandrodeOliveira Is there an advantage of using echo and cat instead of just `cat filename.pid | xargs kill`? – Simon A. Eugster Dec 08 '17 at 11:33
  • 2
    @Simon you don't even need `cat`. Just `xargs -a filename.pid kill` will do. Even if it didn't have that option it is better to use the input redirection operator instead of cat. – Tim Seguine Mar 06 '19 at 08:23
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>

-2

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.

mic4ael
  • 7,974
  • 3
  • 29
  • 42
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • 8
    Your first link does not really answer the question. – Engineer2021 Jul 01 '14 at 16:57
  • 5
    Whilst the linked material may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Toby Speight Jan 15 '18 at 09:21