What is a .pid file and what does it contain?
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
Related videos on Youtube
Comments
-
ifixthat almost 2 years
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?
-
ifixthat over 12 yearsthanks 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 ??
-
Rafael Steil over 12 yearsYes, 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.
-
Shnatsel almost 11 yearsWhy 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?
-
user4815162342 over 10 years@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/…
-
Shnatsel over 10 yearsIn 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.
-
Engineer2021 almost 10 yearsYour first link does not really answer the question.
-
eestrada over 8 years@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. -
skywalker over 8 years@Shnatsel Pid files are not just linux, all unix systems have them.
-
Alexandro de Oliveira almost 7 yearsJust one thing:
echo filename.pid | xargs kill
will not kill the process. I guess you meant:echo $(cat filename.pid) | xargs kill
orecho <process ID> | xargs kill
. -
Simon A. Eugster over 6 years@AlexandrodeOliveira Is there an advantage of using echo and cat instead of just
cat filename.pid | xargs kill
? -
Toby Speight over 6 yearsWhilst 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.
-
Tim Seguine about 5 years@Simon you don't even need
cat
. Justxargs -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.