如何检查进程 ID (PID) 是否存在
在 bash 脚本中,我想要执行以下操作(以伪代码形式):
if [ a process exists with $PID ]; then
kill $PID
fi
条件语句的适当表达式是什么?
In a bash script, I want to do the following (in pseudo-code):
if [ a process exists with $PID ]; then
kill $PID
fi
What's the appropriate expression for the conditional statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
最好的方法是:
kill -0 $PID 的问题是,即使进程正在运行并且您没有杀死它的权限,退出代码也将不为零。例如:
并且
具有非零退出代码,对于普通用户来说无法区分,但其中一个是假设运行的,而另一个则不是。
AnrDaemon 提供的部分相关附加信息:init 进程(PID 1)肯定在所有 Linux 计算机上运行,但并非所有 POSIX 系统都是 Linux。不保证 PID 1 存在:
讨论
如果测试的主体是“kill”,那么讨论kill 和竞争条件的答案是完全正确的。我来寻找一般性的“如何测试 bash 中是否存在 PID”。
/proc
方法很有趣,但在某种意义上破坏了ps
命令抽象的精神,即您不需要去查找/proc< /code> 因为如果 Linus 决定调用
exe
文件其他内容怎么办?The best way is:
The problem with
kill -0 $PID
is that the exit code will be non-zero even if the process is running and you don't have permission to kill it. For example:and
have a non-zero exit codes that are indistinguishable for a normal user, but one of them is by assumption running, while the other is not.
Partly related, additional info provided by AnrDaemon: The init process (PID 1) is certainly running on all Linux machines, but not all POSIX systems are Linux. PID 1 is not guaranteed to exist there:
DISCUSSION
The answers discussing kill and race conditions are exactly right if the body of the test is a "kill". I came looking for the general "how do you test for a PID existence in bash".
The
/proc
method is interesting, but in some sense breaks the spirit of theps
command abstraction, i.e. you don't need to go looking in/proc
because what if Linus decides to call theexe
file something else?要检查进程是否存在,请使用
But just as @unwind said,如果您希望它在任何情况下终止,然后就
否则,您将遇到竞争条件,该进程可能在第一个
kill -0
之后消失。如果您想忽略
kill
的文本输出并根据退出代码执行某些操作,您可以To check for the existence of a process, use
But just as @unwind said, if you want it to terminate in any case, then just
Otherwise you will have a race condition, where the process might have disappeared after the first
kill -0
.If you want to ignore the text output of
kill
and do something based on the exit code, you can在实现 procfs 接口的系统(例如 Linux)上,您只需检查
/proc/ $PID
存在:否则您可以使用
ps
程序:在后一种形式中,
-o pid=
是一种输出格式,仅显示进程 ID 列没有标题。对于非空字符串运算符,引号是必需的 -n
给出有效结果。On systems that implement procfs interface such as Linux, you can just check if
/proc/$PID
exists:otherwise you can use
ps
program:In the latter form,
-o pid=
is an output format to display only the process ID column with no header. The quotes are necessary for non-empty string operator-n
to give valid result.ps
命令与-p $PID
可以做到这一点:ps
command with-p $PID
can do this:您有两种方法:
首先在我的笔记本电脑中查找特定应用程序:
现在所有示例都将查找 PID
3358
。第一种方法:针对第二列中的PID运行
ps aux
和grep
。在此示例中,我查找firefox
,然后查找它的 PID:因此您的代码将是:
第二种方式:只需在
/proc/$PID
目录。我在本例中使用exe
,但您可以使用其他任何东西。所以你的代码将是:
BTW:
kill -9 $PID || 有什么问题吗?真的吗?
编辑:
经过几个月的思考......(大约24......)我在这里给出的最初想法是一个很好的技巧,但非常不可移植。虽然它教授了 Linux 的一些实现细节,但它无法在 Mac、Solaris 或 *BSD 上运行。它甚至可能在未来的 Linux 内核上失败。请 - 按照其他中所述使用“ps”回复。
You have two ways:
Lets start by looking for a specific application in my laptop:
All examples now will look for PID
3358
.First way: Run
ps aux
andgrep
for the PID in the second column. In this example I look forfirefox
, and then for it's PID:So your code will be:
Second way: Just look for something in the
/proc/$PID
directory. I am usingexe
in this example, but you can use anything else.So your code will be:
BTW: whats wrong with
kill -9 $PID || true
?EDIT:
After thinking about it for a few months.. (about 24...) the original idea I gave here is a nice hack, but highly unportable. While it teaches a few implementation details of Linux, it will fail to work on Mac, Solaris or *BSD. It may even fail on future Linux kernels. Please - use "ps" as described in other responses.
我认为这是一个糟糕的解决方案,会引发竞争条件。如果进程在测试和调用终止之间终止怎么办?那么kill就会失败。那么为什么不尝试在所有情况下执行kill,并检查它的返回值以了解它是如何进行的呢?
I think that is a bad solution, that opens up for race conditions. What if the process dies between your test and your call to kill? Then kill will fail. So why not just try the kill in all cases, and check its return value to find out how it went?
看起来你想要
当
$pid
完成时返回。否则,您可以使用它
来检查进程是否仍然存在(这比
kill -0 $pid
更有效,因为即使您不拥有 pid,它也能工作)。It seems like you want
which will return when
$pid
finishes.Otherwise you can use
to check if the process is still alive (this is more effective than
kill -0 $pid
because it will work even if you don't own the pid).例如,在 GNU/Linux 中,您可以使用:
或类似的内容
,它会显示应用程序的名称,只是不带 ID 的名称。
For example in GNU/Linux you can use:
Or something like
and that shows you the name of the app, just the name without ID.
我在这里学习并赞成@FDS的答案,因为它很好而且正确。但是,我发现这是一个更容易阅读和理解的表格。
所以,这是我的首选版本:
说明:
"$?"
部分表示“上一个命令的退出或返回代码”。"$pid"
) 正在运行,并且某些情况下,ps --pid "$pid"
命令将返回退出代码0
如果没有则其他号码。> /dev/null
丢弃所有打印到stdout
的输出,因为我们不想看到它。相反,我们想要的只是ps --pid "$pid"
命令的退出代码 ("$?"
),以查看该 PID 是否正在运行。更具体地说,使用> /dev/null
将stdout
输出重定向到/dev/null
伪文件,其目的是丢弃所有传入的输入。运行 shellcheck path/to/this_script.sh 告诉我实际上应该以其他方式执行此操作(如 @FDS 所示)以避免冗余。请参阅此处的
shellcheck
输出:特别注意这部分:
请在此处查看此错误代码的完整描述:https://github.com/koalaman/shellcheck/维基/SC2181。
因此,
shellcheck
建议采用这种形式:如果您想适应这种方式,那就去做吧。但是,如果您愿意按照我的方式进行操作,因为您认为我的方式更容易阅读和理解,就像我一样,那么您可以选择通过添加
# shellcheck disable= 来禁用该
shellcheck
警告。 SC2181 位于该行上方,如下所示:我的最终首选答案
然而,这里的关键要点是永远不要插入任何命令,甚至不包括
echo 或
print
语句,在被检查的命令(在本例中为ps
)和使用"$?"
检查错误代码之间,或者否则检查错误代码将意外地检查错误代码来自最新命令,例如插入的echo
或print
语句,而不是来自感兴趣的命令(在我们的例子中是ps
)!这就是
shellcheck
推荐他们所做的事情的主要原因:他们希望确保您正在检查错误代码的正确命令。除此之外,它只是一个迂腐的基于意见的讨论,相当于 C 中关于您应该如何检查 NULL(空指针)值的争论:
所以,只是在 C 中选择哪种方式检查 NULL 值(null ptrs)纯粹是一个品味问题,在 bash 中选择哪种方式检查错误代码/返回代码也纯粹是一个问题品尝。目前,我更喜欢上面我标记为“我的最终和首选答案”的版本。
无论如何,这是一个完整的、可运行的程序
check_if_pid_exists.sh 来自我的 eRCaGuy_hello_world 存储库:
示例运行调用和输出:
您可以通过首先运行
ps aux
并选择要传递给它的 PID 来找到要发送到我的脚本的有效 PID(进程 ID)。相关
I learned from and upvoted @FDS's answer here, because it is good and correct. But, here's a form I find easier to read and understand.
So, here is my preferred version:
Explanation:
"$?"
part means "the exit or return code from the previous command".ps --pid "$pid"
command returns exit code0
if the specified PID ("$pid"
) is running, and some other number if not.> /dev/null
discards all output printed tostdout
, since we don't want to see it. Rather, all we want is the exit code ("$?"
) from theps --pid "$pid"
command to see if that PID is running. More specifically, using> /dev/null
redirectsstdout
output to the/dev/null
pseudofile, whose purpose is to discard all incoming input.Running
shellcheck path/to/this_script.sh
tells me I should actually do it the other way (as @FDS shows) to avoid redundancy. See theshellcheck
output here:Notice especially this part:
See the full description for this error code here: https://github.com/koalaman/shellcheck/wiki/SC2181.
So,
shellcheck
recommends this form instead:If you want to adjust to doing it that way, go for it. But, if you'd rather do it my way because you consider my way easier to read and understand, as I do, then you can optionally disable that
shellcheck
warning by adding# shellcheck disable=SC2181
above that line, like this:My final and preferred answer
The key takeaway here, however, is to never insert any command, not even an
echo
orprint
statement, between the command being checked (ps
in this case) and checking the error code with"$?"
, or else checking the error code will accidentally check the error code from the latest command, such as the insertedecho
orprint
statement, instead of from the command of interest (ps
in our case)!That's the main reason
shellcheck
recommends what they do: they want to make sure you're checking the error code of the correct command.Other than that, it's just a pedantic opinion-based-discussion-equivalent to the argument in C about how you should check for
NULL
(null pointer) values:So, just as which way you choose to check for
NULL
values (null ptrs) in C is purely a matter of taste, which way in bash you choose to check for error codes/return codes is also purely a matter of taste. Currently, I prefer the version above I have marked as "My final and preferred answer".Anyway, here is a full, runnable program
check_if_pid_exists.sh from my eRCaGuy_hello_world repo:
Sample run calls and output:
You can find valid PIDs (Process IDs) to send to my script by first running
ps aux
and choosing a PID to pass to it.Related
按pid:
按名称:
“-x”表示“完全匹配”。
By pid:
By name:
"-x" means "exact match".
在这里,我将 PID 存储在名为 .pid 的文件中(有点像 /run/...),并且仅在尚未执行的情况下执行脚本。
注意:存在竞争条件,因为它不检查该 pid 的调用方式。如果系统重新启动并且 .pid 存在但被不同的应用程序使用,这可能会导致“不可预见的后果”。
here i store the PID in a file called .pid (which is kind of like /run/...) and only execute the script if not already being executed.
note: there is a race condition as it does not check how that pid is called. if the system is rebooted and .pid exists but is used by a different application this might lead 'unforeseen consequences'.