如何通过名称而不是PID杀死进程?

时间:2020-03-06 15:00:36  来源:igfitidea点击:

例如,有时当我尝试启动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()执行的进程(因此,它显示的唯一进程是其自身。)

解决方案

pkill firefox

详细信息:http://linux.about.com/library/cmd/blcmdl1_pkill.htm

我通常使用killall命令,有关详细信息,请参见此处。

我们可以使用killall <name>按名称杀死进程。

killall sends 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. -HUP or -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.

但是,如果我们没有看到使用ps aux的过程,那么我们可能无权杀死它。

如果运行GNOME,则可以像在Windows中一样使用系统监视器(系统->管理->系统监视器)来终止进程。 KDE会有类似的东西。

更长的选择:

kill `pidof firefox`