2013-03-12 21 views
2

我人在运行此命令:杀死自动处理 - Linux的

pgrep -l someprocess 

我得到一些输出XXXX someprocess

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

有人可以帮助?

回答

5

您可以使用或者pkillkillall即可完成此操作。

我找到了this简短明了的总结,说明了kill ing过程的不同方式。

pkill就像这样简单:pkill someprocess

@ewm在他的回答中已经包含了关于killall的详细说明,所以我在此不再重复。

+0

谢谢,我不知道! – user1611830 2013-03-12 18:28:36

+0

只是好奇,如果我不得不用手写,我会怎么做? – user1611830 2013-03-12 18:29:23

+0

查看我的更新回答 – shx2 2013-03-12 19:10:52

3

你可能想看看 'killall' 命令:

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

名称 killall - 的名字来杀死

概要 killall [流程-Z, - 上下文模式] [-e, - exact] [-g, - process-group] [-i, - interactive] [-q, - quiet] [-r, - regexp ] [-s, - 信号信号] [-u, - 用户用户] [-v, - 详细] [-w, - wait] [-I, - ignore-case] [-V, --version] [ - ]名称... killall -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). 
+0

+1'killall'更加无处不在。许多平台没有'pkill'。 'sed' /'grep','ps'和'kill'也是选项。 'kill -9 $(ps -ef | grep [Tt] arget | sed -e's/[A-Za-z] * * \([0-9] [0-9] * \)。*/\ 1 /')'会在捏,但更脆。 – 2013-03-24 23:44:35