bash shell 脚本中的 `kill -0 $pid` 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11012527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does `kill -0 $pid` in a shell script do?
提问by gjain
采纳答案by dwalter
sending the signal 0
to a given PID
just checks if any process with the given PID
is running and you have the permission to send a signal to it.
向0
给定的发送信号PID
只是检查是否有给定的进程PID
正在运行,并且您有权向它发送信号。
For more information see the following manpages:
有关更多信息,请参阅以下联机帮助页:
kill(1)杀死(1)$ man 1 kill
...
If sig is 0, then no signal is sent, but error checking is still performed.
...
kill(2)杀死(2)$ man 2 kill
...
If sig is 0, then no signal is sent, but error checking is still performed; this
can be used to check for the existence of a process ID or process group ID.
...
回答by Todd A. Jacobs
This is a Good Question Because...
这是个好问题,因为...
...it can be hard to find documentation on this special signal. Despite what others have said, the only mention of this signal in man 1 kill
in Debian-based systems is:
...很难找到有关此特殊信号的文档。不管其他人怎么说,在man 1 kill
基于 Debian 的系统中唯一提到这个信号的是:
Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0.
特别有用的信号包括 HUP、INT、KILL、STOP、CONT 和 0。
Not especially helpful, especially if you don't already know what the signal does. It is also not listed by the output of kill -l
, so you won't know about it unless you already know about it.
不是特别有用,特别是如果您还不知道信号的作用。它也没有在 的输出中列出kill -l
,所以除非你已经知道它,否则你不会知道它。
Where to Find It Documented
在哪里可以找到记录
On Debian and Ubuntu systems, the output of man 2 kill
says, in part:
在 Debian 和 Ubuntu 系统上,输出部分man 2 kill
说:
If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.
如果 sig 为 0,则不发送信号,但仍进行错误检查;这可用于检查进程 ID 或进程组 ID 是否存在。
What It's Good For
有什么用
You can use kill -0
to check whether a process is running. Consider these examples.
您可以使用它kill -0
来检查进程是否正在运行。考虑这些例子。
# Kill the process if it exists and accepts signals from
# the current user.
sleep 60 &
pid=$!
kill -0 $pid && kill $pid
# Check if a PID exists. When missing, this should result
# in output similar to:
# bash: kill: (6228) - No such process
# Exit status: 1
kill -0 $pid; echo "Exit status: $?"
You can also use kill -0
to determine if the current user has permissions to signal a given process. For example:
您还可以使用kill -0
来确定当前用户是否有权向给定进程发送信号。例如:
# See if you have permission to signal the process. If not,
# this should result in output similar to:
# bash: kill: (15764) - Operation not permitted
# Exit status: 1
sudo sleep 60 &
kill -0 $!; echo "Exit status: $?"
回答by Fritz G. Mehner
This command checks wether the process with PID in $pid is alive.
此命令检查 $pid 中带有 PID 的进程是否处于活动状态。
回答by Sandeep_black
Kill -0 $pid is to check whether the process with pid is existing or not.
kill -0 $pid 是检查带有pid的进程是否存在。
Be careful while using 'kill -0 $pid' to check the process existence because
使用 'kill -0 $pid' 检查进程是否存在时要小心,因为
Once the intended process exit then its pid can be allot to other newly created process. ( So one can not be so sure that particular process is alive or not )
In case of Zombie process, for which child is waiting for parent to call wait. Here it hold the $pid and give the positive result while that process is not running.
一旦预期的进程退出,它的 pid 就可以分配给其他新创建的进程。(因此不能确定特定进程是否存在)
在僵尸进程的情况下,哪个子进程正在等待父进程调用等待。在这里,它保存 $pid 并在该进程未运行时给出正结果。
回答by Sandeep_black
Kill -0 $pid used to check if a process running with $pid is alive or not. But this can be tricky, as process ID can be reassigned, once a process exit and new process runs. One can use killall -0 to get about a particular process is running or not.
Kill -0 $pid 用于检查使用 $pid 运行的进程是否处于活动状态。但这可能很棘手,因为一旦进程退出并运行新进程,就可以重新分配进程 ID。可以使用 killall -0 来了解特定进程是否正在运行。
回答by Anthony Rutledge
Sending the EXIT
signal, or 0
to a process will:
发送EXIT
信号或发送0
到进程将:
- Check for the existence of a process.
- Do various error checking on the process (PID, PGID, etc ...).
- It will not send any output to
stdout
upon success. - Send an error message to
stderr
if something is not right. - Give you a false positive if the process is defunct (i.e. Zombie).
- 检查进程是否存在。
- 对进程进行各种错误检查(PID、PGID 等...)。
stdout
成功后不会向其发送任何输出。stderr
如果出现问题,请向 发送错误消息。- 如果该过程已失效(即僵尸),则给您一个误报。
More explicitly, a useful function for your shell scripts would be:
更明确地说,对您的 shell 脚本有用的函数是:
function isProcess ()
{
kill -s EXIT 2> /dev/null
}
This returns no text to stdout
upon success, but an error message to stderr
upon failure (but I have redirected that error message to /dev/null
).
这stdout
在成功时不返回文本,但stderr
在失败时返回错误消息(但我已将该错误消息重定向到/dev/null
)。
If you are concerned about defunct / zombie process status, then you need to use ps
, preferably with the --no-headers
switch.
如果您担心 defunct/zombie 进程状态,那么您需要使用ps
,最好与--no-headers
开关一起使用。
#!/bin/ksh
function trim ()
{
echo -n "" | tr -d [:space:]
}
function getProcessStatus ()
{
trim $(ps -p -o stat --no-headers)
}
function isZombie ()
{
typeset processStatus=$(getProcessStatus )
[[ "$processStatus" == "Z" ]]
return $?
}