Many programs such as sshd create .pid files in /var/run/ that contain their process ID. Are these files reliable for determining whether a process is running? My guess is that these files are created manually by a process, and therefore will still remain in the file system if the program crashes.
migrated from stackoverflow.com Feb 23 '10 at 16:04
|
in simple terms, no: a process (e.g. a daemon) can crash and not have the time to clear its .pid file. A technique to be more certain of the state of a program: use an explicit communication channel such as a socket. Write the socket port in a file and have the You can also use the services of DBus on Linux: register a specific name and have your supervisor process (whatever you call it) check for that name. There are numerous techniques. One thing to remember: it is not the OS' responsibility to manage the PID files. |
|||||||||||
|
|
Jldupont is correct in stating that .pid files are not reliable for determining whether a process is running as the file may not be removed in the event of a crash. Race conditions aside, I often use pgrep when I need to know if a process is running. I could then cross-reference the output against the .pid file(s) if I felt it necessary. |
|||
|
|
|
Jldupont is correct. You can, however, send the process a 0 signal (kill -s 0 pid) to see if the process is still alive (assuming you have the authority to send such a signal -- in general, only the owner of a process may send it a signal). |
|||||
|
|
I agree with jschmier. On some systems, you do not get access to pgrep. In such a case, you can do |
|||||||||||
|