在Unix环境中检测过时的pid文件 [英] Detecting a stale pid file in a Unix environment

查看:75
本文介绍了在Unix环境中检测过时的pid文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unix环境中检测过时的pid文件的标准,跨平台方法是什么?说我想杀死我的应用程序的旧实例,但是如果那个应用程序已经退出,我当然不想破坏具有相同PID的不相关进程.

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.

现在,我找到了一种在Ubuntu(以及其他可能基于GNU/Linux的系统)上执行此操作的方法-以下伪代码:

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 */

但是这样的黑客可以在其他系统上工作吗?无论如何,这应该是具有30多年已知解决方案的标准任务.

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.

谢谢.

推荐答案

/proc/处的文件时间回溯至至少1991 ;参见第243.而且,尽管可以伪造mtime(例如,通过touch -m --date=<needed_date> <target_file>进行伪造),但是对于userland应用而言,无法在其自己的/proc/条目中进行伪造.

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.

这篇关于在Unix环境中检测过时的pid文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆