2

我正在运行这个命令:

pgrep -l someprocess

我得到一些输出XXXX someprocess

然后我手动杀死每个出现的进程,我想编写一个脚本来自动执行它,但这没有(没有意义 kill -9 $(pgrep -l someprocess | grep "^[0-9]{4}")

有人可以帮忙吗?

4

2 回答 2

5

您可以使用pkillkillall完全实现这一点。

我发现这个简短而清晰的总结解释了不同的kill编程方式。

pkill很简单:pkill someprocess.

@ewm 已经在他的回答中包含了详细的解释killall,所以我在这里不再重复。

于 2013-03-12T18:15:38.080 回答
3

您可能想查看“killall”命令:

KILLALL(1) 用户命令 KILLALL(1)

NAME killall - 按名称杀死进程

概要 killall [-Z,--context pattern] [-e,--exact] [-g,--process-group] [-i,--interactive] [-q,--quiet] [-r,- -regexp] [-s,--signal 信号] [-u,--user 用户] [-v,--verbose] [-w,--wait] [-I,--ignore-case] [-V ,--version] [--] 名称 ... killall -l killall -V,--version

描述 killall 向运行任何指定命令的所有进程发送信号。如果未指定信号名称,则发送 SIGTERM。

   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 execut-
   ing that particular file will be selected for killing, independent of their name.

   killall returns a zero return code if at least one process has been killed for each  listed  command,
   or  no  commands  were listed and at least one process matched the -u and -Z search criteria. killall
   returns non-zero otherwise.

   A killall process never kills itself (but may kill other killall processes).
于 2013-03-12T18:16:00.517 回答