Detecting a stale pid file in a Unix environment

寵の児 提交于 2019-12-25 14:12:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/40205638/detecting-a-stale-pid-file-in-a-unix-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!