bash 如果存在,则杀死进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17049183/
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-18 05:39:38  来源:igfitidea点击:

Kill a process if it exists

bashshellkill-process

提问by SirRupertIII

I want to kill all instruments processes that are running. I am using this:

我想杀死所有正在运行的仪器进程。我正在使用这个:

sudo killall instruments

sudo killall instruments

I am using this in a script and sometimes the process is not running and it stops my script saying there are no processes with that name that are running.

我在脚本中使用它,有时进程没有运行,它会停止我的脚本,说没有正在运行的具有该名称的进程。

How can I check if a particular process is running? Instruments in my case.

如何检查特定进程是否正在运行?仪器在我的情况下。

采纳答案by KamikazeCZ

sudo killall instruments 2>/dev/null

isn't enough?

不够?

回答by that other guy

If your script is terminating, you have most likely enabled set -e, to exit when a command fails.

如果您的脚本正在终止,您很可能已启用set -e, 以在命令失败时退出。

If you don't care about the status, you can just append || trueto the command:

如果你不关心状态,你可以附加|| true到命令:

sudo killall instruments || true

回答by giordano

You can use pgrep <proc>to search for a process named <proc>

您可以使用pgrep <proc>搜索名为的进程<proc>

if pgrep instruments &> /dev/null ; then sudo killall instruments ; fi