【问题标题】:Killing processes automatically - linux自动杀死进程 - linux
【发布时间】:2013-03-12 18:13:20
【问题描述】:

我正在运行这个命令:

pgrep -l someprocess

我得到了一些输出XXXX someprocess

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

有人可以帮忙吗?

【问题讨论】:

    标签: linux grep kill-process


    【解决方案1】:

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

    我发现 this 简短而清晰的摘要解释了 killing 进程的不同方式。

    pkill 很简单:pkill someprocess

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

    【讨论】:

    • 只是出于好奇,如果我必须手写,我会怎么做?
    【解决方案2】:

    您可能想查看“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).
    

    【讨论】:

    • +1 killall 更为普遍。许多平台没有pkillsed/greppskill 也是选项。 kill -9 $(ps -ef | grep [Tt]arget | sed -e 's/[A-Za-z]* *\([0-9][0-9]*\).*/\1/') 会在紧要关头做,但更脆弱。
    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多