1

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.

Community
  • 1
  • 1
Dallaylaen
  • 5,268
  • 20
  • 34

1 Answers1

2

File times at /proc/ date back to at least 1991; see p. 243. And, although mtime can be faked (e.g. via touch -m --date=<needed_date> <target_file>), for a userland app there is no way to do so on its own /proc/ entry.

hidefromkgb
  • 5,834
  • 1
  • 13
  • 44