在 Linux 上如何通过名称而不是 PID 来终止进程?
有时,当我尝试启动 Firefox 时,它会显示“Firefox 进程已在运行”。 所以我必须这样做:
jeremy@jeremy-desktop:~$ ps aux | grep firefox
jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/firefox-3.0.1/firefox
jeremy 7578 0.0 0.3 3004 768 pts/0 S+ 22:44 0:00 grep firefox
jeremy@jeremy-desktop:~$ kill 7451
我想要的是一个可以为我做这一切的命令。 它将在进程列表中获取输入字符串和 grep(或其他内容),并杀死输出中的所有进程:
jeremy@jeremy-desktop:~$ killbyname firefox
我尝试在 PHP 中执行此操作,但 exec(' ps aux') 似乎只显示在 PHP 脚本本身中使用 exec()
执行的进程(因此它显示的唯一进程就是它本身。)
Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:
jeremy@jeremy-desktop:~$ ps aux | grep firefox
jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/firefox-3.0.1/firefox
jeremy 7578 0.0 0.3 3004 768 pts/0 S+ 22:44 0:00 grep firefox
jeremy@jeremy-desktop:~$ kill 7451
What I'd like is a command that would do all that for me. It would take an input string and grep
for it (or whatever) in the list of processes, and would kill all the processes in the output:
jeremy@jeremy-desktop:~$ killbyname firefox
I tried doing it in PHP but exec('ps aux')
seems to only show processes that have been executed with exec()
in the PHP script itself (so the only process it shows is itself.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
更多信息:http://linux.about.com/library/cmd/blcmdl1_pkill.htm< /a>
More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm
也可以使用:
对我来说,它的效果非常好。 这就是我一直在寻找的东西。
pkill 不适用于没有标志的名称。
当设置
-f
时,完整的命令行用于模式匹配。Also possible to use:
For me, it worked up perfectly. It was what I have been looking for.
pkill doesn't work with name without the flag.
When
-f
is set, the full command line is used for pattern matching.您可以使用
killall< 按名称终止进程/代码>
但是,如果您没有使用
ps aux
看到该进程,您可能无权杀死它......You can kill processes by name with
killall <name>
But if you don't see the process with
ps aux
, you probably won't have the right to kill it ...最简单的方法是首先检查您是否获得了正确的进程 ID:
如果结果符合预期。 继续:
如果进程卡住并且无法完成请求,您可以使用kill。
如果您想安全起见并且只终止您最初启动的进程,请添加
-u
以及您的用户名The easiest way to do is first check you are getting right process IDs with:
If the result is as expected. Go with:
If processes get stuck and are unable to accomplish the request you can use kill.
If you want to be on the safe side and only terminate processes that you initially started add
-u
along with your username更长一点的替代方案:
A bit longer alternative:
终止启动路径中具有
snippet
的所有进程。您可以通过将 /directory/ 作为代码段来终止从某个目录启动的所有应用程序。 当您从同一应用程序目录启动同一应用程序的多个组件时,这非常有用。* 如果可用的话我更喜欢 pgrep
Kill all processes having
snippet
in startup path. You can kill all apps started from some directory by for putting /directory/ as a snippet. This is quite usefull when you start several components for the same application from the same app directory.* I would prefer pgrep if available
奇怪,但我还没有看到这样的解决方案:
它还可以杀死多个进程(多个pid),例如:
我更喜欢
pidof
,因为它有单行输出:Strange, but I haven't seen the solution like this:
it can also kill multiple processes (multiple pids) like:
I prefer
pidof
since it has single line output:使用
killall
命令:使用
-9
或-KILL
强行杀死程序(选项与kill
类似) > 命令)。Using
killall
command:Use
-9
or-KILL
to forcefully kill the program (the options are similar to thekill
command).在 Mac 上,我找不到 pgrep 和 pkill,killall 也不起作用,因此编写了一个简单的单行脚本:-
如果有更简单的方法来执行此操作,请分享。
On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-
If there's an easier way of doing this then please share.
使用 grep 杀死:
To kill with grep:
更正确的是:
more correct would be:
我通常使用
killall
命令。查看此链接了解此命令的详细信息。
I normally use the
killall
command.Check this link for details of this command.
我问自己同样的问题,但当前答案的问题是它们不能安全检查要终止的进程,所以...这可能会导致可怕的错误:)...尤其是如果有多个进程与该模式匹配。
作为免责声明,我不是 sh 专业人士,而且肯定还有改进的空间。
所以我写了一个sh脚本:
I was asking myself the same question but the problem with the current answers is that they don't safe check the processes to be killed so... it could lead to terrible mistakes :)... especially if several processes matches the pattern.
As a disclaimer, I'm not a sh pro and there is certainly room for improvement.
So I wrote a little sh script :
Kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')
kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')
如果您运行 GNOME,则可以使用系统监视器(系统 -> 管理 -> 系统监视器)来终止进程,就像在 Windows 下一样。 KDE 也会有类似的东西。
If you run GNOME, you can use the system monitor (System->Administration->System Monitor) to kill processes as you would under Windows. KDE will have something similar.
默认的
kill
命令接受命令名称作为 PID 的替代。 请参阅kill (1)。 经常出现的问题是bash
提供了自己的kill
,它接受作业编号,例如kill %1
,但不接受命令名称。 这会妨碍默认命令。 如果前一个功能比后者对您更有用,您可以通过调用enable -n Kill
禁用
bash
版本。有关详细信息,请参阅kill
> 和 bash 中的enable
条目(1)。The default
kill
command accepts command names as an alternative to PID. See kill (1). An often occurring trouble is thatbash
provides its ownkill
which accepts job numbers, likekill %1
, but not command names. This hinders the default command. If the former functionality is more useful to you than the latter, you can disable thebash
version by callingenable -n kill
For more info see
kill
andenable
entries in bash (1).awk oneliner,它解析 ps 输出的标题,因此您不需要关心列号(但列名)。 支持正则表达式。 例如,要终止所有可执行文件名称(不带路径)包含单词“firefox”的进程,请尝试
awk oneliner, which parses the header of
ps
output, so you don't need to care about column numbers (but column names). Support regex. For example, to kill all processes, which executable name (without path) contains word "firefox" try