Why I should use a pidfle?

I use a program that use a pid file. My question is when it is usefull to use a pid file?

1 Like

Depending on the program, and whether it’s a properly written systemd service, you may find the pid file is not needed.

A pid or “process identifier” is typically used by a program that is running as a daemon or service. It contains the process id of the running instance program. If this particular program forks multiple instances of it and there is a lone number in the file it is usually the id of the parent over any of the children. In Unix/Linux et al, one can use the kill command to send signals of different types to the process and it trap it and it may take certain actions. A common on is “SIGHUP”, integer value 1 or the argument -HUP to the kill command, which tells the recipient to “hangup” and re-read the configuration file. This is less than SIGTERM / -TERM which means “terminate”. You may be familiar with -9 or -SIGKILL or -KILL which is untrappable and a receiving process will be ended by the kernel. The process can not do anything about it. A less known one is SIGSTOP, which can be trapped and the kernel will stop the process. When the same process receives SIGCONT it will continue. I have found this useful in certain situation such as receiving network connections to cause a stall and reproduce certain network conditions useful for debugging a service.

1 Like

Useful trivia is that SIGHUP makes ‘bash’ save its command-line history (and propagate SIGHUP to its children) and exit, while with SIGTERM no history is saved when bash exits.

A pidfile has predictable (predetermined) name, so it is trivial to find (if it exists). Then one can “dereference” it to find the running process (that does not have predictable pid).