Linux下检测进程是否存在
2014-09-21 16:32
495 查看
这个问题看起来好像很简单,"ps -ef | grep xx"一下就行啦!这样做当然可以,但是如果我们考究起性能来,这恐怕不是个好办法。
假设我们现在要监测某进程是否存活,每分钟检查一次,用上面的办法就要每分钟运行一次ps命令并且做一次grep正则查找。这点开销在服务器上似乎不算什么,然而如果我们要在同一节点上同时监测数十个、数百个这样的进程又如何呢?所以,我们有必要从性能的角度出发,发掘一些更好的办法。
对于daemon进程,通常都会有自己的pid或者lock文件,我们可以检查这些文件是否存在来判断进程是否存在。然而有些异常情况下,pid文件存在进程却并不存在。因此并不能依赖进程的pid文件来检测进程是否存活。
一种可靠的方法是使用"kill -0 pid",kill -0不会向进程发送任何信号,但是会进行错误检查。如果进程存在,命令返回0,如果不存在返回1。
但是,这种方法对于普通用户来说只能用于检查自己的进程,因为向其它用户的进程发送信号会因为没有权限而出错,返回值也是1。
当然,如果你用特权用户执行kill命令的话,就没有权限问题啦。
另一方面,我们知道内核会通过/proc虚拟文件系统导出系统中正在运行的进程信息,每个进程都有一个/proc/<pid>目录。因此我们可以将检测进程是否存在转换为检测/proc/<pid>目录是否存在,这样就简单多了。
最后,我们怎么得到进程的pid呢?通常对于daemon进程我们可以从它的pid文件或者lock文件中读取。如果没有pid文件的话,在监控脚本中先用"ps | grep"、pgrep、pidof等命令得到要监控的进程pid,再用上述方法检测就行了。
假设我们现在要监测某进程是否存活,每分钟检查一次,用上面的办法就要每分钟运行一次ps命令并且做一次grep正则查找。这点开销在服务器上似乎不算什么,然而如果我们要在同一节点上同时监测数十个、数百个这样的进程又如何呢?所以,我们有必要从性能的角度出发,发掘一些更好的办法。
对于daemon进程,通常都会有自己的pid或者lock文件,我们可以检查这些文件是否存在来判断进程是否存在。然而有些异常情况下,pid文件存在进程却并不存在。因此并不能依赖进程的pid文件来检测进程是否存活。
一种可靠的方法是使用"kill -0 pid",kill -0不会向进程发送任何信号,但是会进行错误检查。如果进程存在,命令返回0,如果不存在返回1。
<span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span><span style="color: rgb(0, 0, 0); ">$ ps PID TTY TIME CMD </span>15091 pts/0 00:00:00<span style="color: rgb(0, 0, 0); "> bash </span>15943 pts/0 00:00:00<span style="color: rgb(0, 0, 0); "> ps </span><span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span>$ kill -0 15091 <span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span><span style="color: rgb(0, 0, 0); ">$ echo $? </span>0 <span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span>$ kill -0 15092<span style="color: rgb(0, 0, 0); "> -bash: kill: (</span>15092<span style="color: rgb(0, 0, 0); ">) - No such process </span><span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span><span style="color: rgb(0, 0, 0); ">$ echo $? </span>1 <span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span>$
但是,这种方法对于普通用户来说只能用于检查自己的进程,因为向其它用户的进程发送信号会因为没有权限而出错,返回值也是1。
<span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span>$ kill 2993<span style="color: rgb(0, 0, 0); "> -bash: kill: (</span>2993<span style="color: rgb(0, 0, 0); ">) - Operation not permitted </span><span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span><span style="color: rgb(0, 0, 0); ">$ echo $? </span>1 <span style="color: rgb(128, 0, 0); "><strong>[</strong></span><span style="color: rgb(128, 0, 0); ">sw@gentoo ~</span><span style="color: rgb(128, 0, 0); "><strong>]</strong></span>$
当然,如果你用特权用户执行kill命令的话,就没有权限问题啦。
另一方面,我们知道内核会通过/proc虚拟文件系统导出系统中正在运行的进程信息,每个进程都有一个/proc/<pid>目录。因此我们可以将检测进程是否存在转换为检测/proc/<pid>目录是否存在,这样就简单多了。
最后,我们怎么得到进程的pid呢?通常对于daemon进程我们可以从它的pid文件或者lock文件中读取。如果没有pid文件的话,在监控脚本中先用"ps | grep"、pgrep、pidof等命令得到要监控的进程pid,再用上述方法检测就行了。
相关文章推荐
- Linux下检测进程是否存在
- Linux下检测进程是否存在
- linux 检测进程是否存在
- linux下检测进程是否存在的标准做法
- linux 检测进程是否存在,并统计进程数的有用的方法和脚本
- Linux下检测进程是否存在
- 批处理和VBS检测进程是否存在并执行相关操作
- shell 查看Linux 进程 是否存在
- java 检测进程是否存在
- 用Linux守护进程检测某个程序是否运行2
- Linux判断进程是否存在并启动该进程
- vb 检测进程是否存在
- Linux 检测Python程序是否存在并自动重启
- 在系统中检测指定的窗口是否已经打开,检查某进程是否存在
- 在系统中检测指定的窗口是否已经打开,检查某进程是否存在
- LINUX判断进程是否存在并启动该进程
- Linux下C语言实现查看进程是否存在
- Linux下父进程通过管道发送文件名给子进程,子进程获取文件名后首先判断文件是否存在,不存在,通过管道返回错误信息,存在,将文件内容返回给父进程
- Linux下通过进程名检测进程是否在运行
- 用Linux守护进程检测某个程序是否运行