signal 0 test Kill

上篇文章里列举出了常用的kill signal ,但是没有对于signal 0的描述,这里总结下

首先看下这篇文章  http://www.linuxjournal.com/content/monitoring-processes-kill

Monitoring Processes with Kill

If you have a process ID but aren't sure whether it's valid, you can use the most unlikely of candidates to test it: the kill command. If you don't see any reference to this on the kill(1) man page, check the info pages. The man/info page states that signal 0 is special and that the exit code from kill tells whether a signal could be sent to the specified process (or processes).

So kill -0 will not terminate the process, and the return status can be used to determine whether a process is running. For example:

 $ echo $$     # show our process id
 12833
 $ /bin/bash   # create new process
 $ echo $$     # show new process id
 12902
 $ kill -0 12902
 $ echo $?     # exists, exit code is 0
 0
 $ exit        # return to previous shell
 $ kill -0 12902
 bash: kill: (12902) - No such process
 $ echo $?     # doesn't exist, exit code is 1
 1

Many UNIX dæmons store their process IDs in a file in /var/run when they are started. Using kill -0 to test the pid is a lot easier than parsing ps output. For example, to test whether cron is running, do the following:

 # kill -0 $(cat /var/run/cron.pid)
 # echo $?
 0

下面的评论说出了重点:

Signal 0 not only tests for existance, it also tests for permissions, so to speak. "Am I allowed to send a singal to a given PID?"

再来看下这篇帖子

http://www.linuxquestions.org/questions/linux-newbie-8/what-does-kill-0-pid-do-651484/

it does nothing, but the exit code of your "kill -0 PID" command just returns 0 if you can send a signal to PID, and returns 1 if you can't (don't have access or invalid PID)
 

最后看下kill的文档


http://linux.die.net/man/2/kill

http://pubs.opengroup.org/onlinepubs/009695399/functions/kill.html

man kill page

0          0   n/a       exit code indicates if a signal may be sent

猜你喜欢

转载自dikar.iteye.com/blog/976820
今日推荐