本文翻译自:How can I kill a process by name instead of PID?
Sometimes when I try to start Firefox it says "a Firefox process is already running". 有时当我尝试启动Firefox时,它说“Firefox进程已经在运行”。 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: 它将在进程列表中为它(或其他)获取输入字符串和grep
,并将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')
似乎只显示在PHP脚本本身中使用exec()
执行的进程(因此它显示的唯一进程本身。)
#1楼
参考:https://stackoom.com/question/frY/如何通过名称而不是PID来终止进程
#2楼
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 $1}' | cut -d' ' -f1`;kill $pid
If there's an easier way of doing this then please share. 如果有更简单的方法,请分享。
#3楼
pkill firefox
更多信息: http : //linux.about.com/library/cmd/blcmdl1_pkill.htm
#4楼
我通常使用killall命令查看此处了解详细信息。
#5楼
You can kill processes by name with killall <name>
您可以使用killall <name>
按名称 killall <name>
进程
killall sends a signal to all processes running any of the specified commands. killall向运行任何指定命令的所有进程发送信号。 If no signal name is specified, SIGTERM is sent. 如果未指定信号名称,则发送SIGTERM。
Signals can be specified either by name (eg -HUP or -SIGHUP ) or by number (eg -1 ) or by option -s . 信号可以通过名称(例如-HUP或-SIGHUP )或数字(例如-1 )或选项-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. 如果命令名称不是正则表达式(选项-r )并且包含斜杠(/),则将选择执行该特定文件的进程进行查杀,而与其名称无关。
But if you don't see the process with ps aux
, you probably won't have the right to kill it ... 但如果你没有看到ps aux
的过程,你可能没有权利杀死它...
#6楼
If you run GNOME, you can use the system monitor (System->Administration->System Monitor) to kill processes as you would under Windows. 如果运行GNOME,则可以像使用Windows一样使用系统监视器(系统 - >管理 - >系统监视器)来终止进程。 KDE will have something similar. KDE会有类似的东西。