What is a .pid file and what does it contain?
LinuxUnixPidLinux Problem Overview
I recently come across a file with the extension .pid and explored 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?
Linux Solutions
Solution 1 - Linux
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
Solution 2 - Linux
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>
Solution 3 - Linux
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.
Solution 4 - Linux
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.