bash 使用带有 ps -ax 的 grep 一次杀死多个应用程序 (Mac)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16972397/
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
Using grep with ps -ax to kill numerous applications at once (Mac)
提问by Doug Fir
I'm very new to shell scripting. I recently covered a tutorial of using bash with grep.
我对 shell 脚本很陌生。我最近介绍了使用 bash 和 grep 的教程。
About 5 minutes ago there was, as it happened, a practical application for this function as my browser appeared to have crashed. I wanted to, for practice, use the shell to find the process and then kill it.
大约 5 分钟前,碰巧有此功能的实际应用程序,因为我的浏览器似乎崩溃了。为了练习,我想使用 shell 来查找进程,然后将其杀死。
I typed ps -ax | grep Chrome
我打字 ps -ax | grep Chrome
But many lines appeared with lots of numbers on the left (are these "job numbers" or "process numbers"?)
但是出现了很多行,左边有很多数字(这些是“作业号”还是“进程号”?)
Is there a way to Kill all "jobs" where Chrome exists in the name of the process? Rather than kill each one individually?
有没有办法杀死进程名称中存在 Chrome 的所有“作业”?而不是单独杀死每个人?
回答by ramonovski
I do not recommend using killall.
And you should NOT use -9at first.
我不建议使用killall. 一开始你不应该使用-9。
I recommend a simple pkill:
我推荐一个简单的pkill:
pkill Chrome
回答by Johnny Maclane
I like my solution better. Because adds a kind of wildcard to it. I did this:
我更喜欢我的解决方案。因为给它添加了一种通配符。我这样做了:
mkdir ~/.bin
echo "ps -ef | grep -i | grep -v grep | awk '{print }' | xargs kill -9" > ~/.bin/k
echo "alias k='/Users/YOUR_USER_HERE/.bin/k '" >> ~/.profile
chmod +x ~/.bin/k
Now to kill stuff you type:
现在杀死你输入的东西:
k Chrome
or
或者
k chrome
or
或者
k chr
Just be careful with system processes or not to be killed processes.
只要小心系统进程或不要被杀死进程。
Like if you have Calculator running and another one called Tabulator also running. If you typek ulatorit will kill'em both.
就像你有 Calculator 正在运行而另一个名为 Tabulator 的也正在运行。如果你输入k ulator它会杀死他们两个。
Also remember that this will kill ALL instances of the program(s) that match the variable after the command k.
还要记住,这将杀死与命令后面的变量匹配的程序的所有实例k。
Because of this, you can create two aliases inside your profile file and make 2 files inside .bin one with the command grep -iand the other without it. The one without the -i flag will be case and name sensitive.
因此,您可以在配置文件中创建两个别名,并在 .bin 中创建 2 个文件,一个使用命令grep -i,另一个不使用命令。没有 -i 标志的将区分大小写和名称。
回答by cabad
You can use killall:
你可以使用killall:
killall Chrome
Edit: Removed "-9" since it should be used as a last resource (was: killall -9 Chrome).
编辑:删除了“-9”,因为它应该用作最后一个资源(是:killall -9 Chrome)。

