What is the standard, cross-platform way to detect stale pid file in a Unix environment? Say I would like to kill an old instance of my application, but I certainly don't want to disrupt an unrelated process with the same PID if that application has already exited.
Now I found a way to do it on my Ubuntu (and thus probably other GNU/Linux based systems) - pseudocode below:
if ( mtime(pid_file) < mtime( "/proc/"+pid ) ) {
/* process started AFTER the file creation */
/* so it's not what we're looking for */
unlink(pid_file);
return 0;
};
/* proceed to kill & do all stuff */
But would such hack work on other systems? Anyway it should be a standard task with a solution known for 30+ years.
Found a similar question here, but failed to find a definite answer to my question.
Thank you.