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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:14:33  来源:igfitidea点击:

What does `kill -0 $pid` in a shell script do?

bashshellscriptingsignalskill

提问by gjain

Basically, what signal does '0' represent, because hereI see SIGNAL numbers starting from 1.

基本上,'0'代表什么信号,因为在这里我看到信号数字从1开始。

采纳答案by dwalter

sending the signal 0to a given PIDjust checks if any process with the given PIDis 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 killin 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 killsays, 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 -0to 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 -0to 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' 检查进程是否存在时要小心,因为

  1. 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 )

  2. 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.

  1. 一旦预期的进程退出,它的 pid 就可以分配给其他新创建的进程。(因此不能确定特定进程是否存在)

  2. 在僵尸进程的情况下,哪个子进程正在等待父进程调用等待。在这里,它保存 $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 EXITsignal, or 0to a process will:

发送EXIT信号或发送0到进程将:

  1. Check for the existence of a process.
  2. Do various error checking on the process (PID, PGID, etc ...).
  3. It will not send any output to stdoutupon success.
  4. Send an error message to stderrif something is not right.
  5. Give you a false positive if the process is defunct (i.e. Zombie).
  1. 检查进程是否存在。
  2. 对进程进行各种错误检查(PID、PGID 等...)。
  3. stdout成功后不会向其发送任何输出。
  4. stderr如果出现问题,请向 发送错误消息。
  5. 如果该过程已失效(即僵尸),则给您一个误报。

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 stdoutupon success, but an error message to stderrupon 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-headersswitch.

如果您担心 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 $?
}