【发布时间】:2013-03-12 18:13:20
【问题描述】:
我正在运行这个命令:
pgrep -l someprocess
我得到了一些输出XXXX someprocess
然后我手动杀死每个出现的进程,我想编写一个脚本来自动执行它,但这没有(没有意义
kill -9 $(pgrep -l someprocess | grep "^[0-9]{4}")
有人可以帮忙吗?
【问题讨论】:
标签: linux grep kill-process
我正在运行这个命令:
pgrep -l someprocess
我得到了一些输出XXXX someprocess
然后我手动杀死每个出现的进程,我想编写一个脚本来自动执行它,但这没有(没有意义
kill -9 $(pgrep -l someprocess | grep "^[0-9]{4}")
有人可以帮忙吗?
【问题讨论】:
标签: linux grep kill-process
您可以使用pkill 或killall 来实现这一点。
我发现 this 简短而清晰的摘要解释了 killing 进程的不同方式。
pkill 很简单:pkill someprocess。
@ewm 在他的回答中已经包含了关于killall 的详细解释,所以我在这里不再重复。
【讨论】:
您可能想查看“killall”命令:
KILLALL(1) 用户命令 KILLALL(1)
名称 killall - 按名称杀死进程
概要 killall [-Z,--context pattern] [-e,--exact] [-g,--process-group] [-i,--interactive] [-q,--quiet] [-r,--regexp] [-s,--信号信号] [-u,--user 用户] [-v,--verbose] [-w,--wait] [-I,--ignore-案子] [-V,--version] [--] 名称... 杀戮 -l killall -V,--版本
描述 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).
【讨论】:
killall 更为普遍。许多平台没有pkill。 sed/grep、ps 和 kill 也是选项。 kill -9 $(ps -ef | grep [Tt]arget | sed -e 's/[A-Za-z]* *\([0-9][0-9]*\).*/\1/') 会在紧要关头做,但更脆弱。