Linux 如何按名称而不是 PID 杀死进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160924/
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
How can I kill a process by name instead of PID?
提问by Jeremy Ruten
Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:
有时,当我尝试启动 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
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:
我想要的是一个可以为我做这一切的命令。它将grep
在进程列表中获取一个输入字符串和它(或其他),并将杀死输出中的所有进程:
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.)
我尝试在 PHP 中执行此操作,但exec('ps aux')
似乎只显示已exec()
在 PHP 脚本本身中执行的进程(因此它显示的唯一进程是它自己。)
采纳答案by shoosh
pkill firefox
More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm
回答by Bittercoder
I normally use the killall
command.
我通常使用killall
命令。
Check this linkfor details of this command.
检查此链接以获取此命令的详细信息。
回答by Andre Bossard
You can kill processes by namewith killall <name>
您可以通过杀死进程名称与killall <name>
killallsends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.
Signals can be specified either by name (e.g. -HUPor -SIGHUP) or by number (e.g. -1) or by option -s.
If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.
killall向运行任何指定命令的所有进程发送信号。如果未指定信号名称,则发送 SIGTERM。
信号可以通过名称(例如-HUP或-SIGHUP)或编号(例如 -1)或选项-s 指定。
如果命令名称不是正则表达式(选项-r)并且包含斜杠 (/),则将选择执行该特定文件的进程进行终止,而与其名称无关。
But if you don't see the process with ps aux
, you probably won't have the right to kill it ...
但是,如果您没有看到带有 的进程ps aux
,则您可能无权杀死它......
回答by Bernard
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.
如果您运行 GNOME,您可以使用系统监视器(系统->管理->系统监视器)来终止进程,就像在 Windows 下一样。KDE 也会有类似的东西。
回答by Walter
A bit longer alternative:
更长一点的选择:
kill `pidof firefox`
回答by Dhiraj
On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-
在 Mac 上,我找不到 pgrep 和 pkill 也没有 killall 工作,所以写了一个简单的单行脚本:-
export pid=`ps | grep process_name | awk 'NR==1{print }' | cut -d' ' -f1`;kill $pid
If there's an easier way of doing this then please share.
如果有更简单的方法,请分享。
回答by user2396265
Using #killall
command:
使用#killall
命令:
#killall -9 <processname>
回答by Chadiso
more correct would be:
更正确的是:
export pid=`ps aux | grep process_name | awk 'NR==1{print }' | cut -d' ' -f1`;kill -9 $pid
回答by edW
To kill with grep:
用 grep 杀死:
kill -9 `pgrep myprocess`
回答by Victor
Also possible to use:
也可以使用:
pkill -f "Process name"
For me, it worked up perfectly. It was what I have been looking for. pkill doesn't work with name without the flag.
对我来说,它工作得很好。这是我一直在寻找的。pkill 不适用于没有标志的名称。
When -f
is set, the full command line is used for pattern matching.
当-f
被设置时,完整的命令行用于模式匹配。