【问题标题】:grepping the PID of a process - Unixgrepping进程的PID - Unix
【发布时间】:2016-05-11 19:04:09
【问题描述】:

我正在尝试执行以下命令:

ps aux | grep com.scheduler.app.workermain | kill -15 [pid]

如何使用ps aux | grep "expression" 获取[pid](或PID 列表)并将其通过管道传输到kill?可能有零个或多个进程在运行机器。这是自动化作业的一部分,以确保终止所有旋转的进程。 执行ps aux | grep com.scheduler.app.workermain 时,命令行中的示例行是:

jenkins  12373  1.1  4.2 2905440 173628 ?      Sl   19:28   0:05 java -Xmx600m -Dlog4j.configurationFile=log4j2-trace.xml -Dpid=foobar -Dipaddr=127.0.0.1 -cp build/classes:build/dependency/* com.scheduler.app.workermain testing.properties

【问题讨论】:

  • 您没有pkill 可用吗?
  • 48 项,当您在 S.O. 上搜索 grep PID of a process [Unix] 时祝你好运。

标签: linux bash unix grep pipe


【解决方案1】:

pkill 正是用于此目的。怎么样:

pkill -15 -f com.scheduler.app.workermain

此外,如果您只想使用 grep 获取 PID,您可以使用 pgrep

pgrep -f com.scheduler.app.workermain

pkill man page

【讨论】:

    【解决方案2】:
    kill -15 $(ps aux | grep -i com.scheduler.app.workermain | awk -F' ' '{ print $2 }')
    

    【讨论】:

    • 正是我的想法。
    【解决方案3】:

    一种可能的解决方案是使用pidof 命令:

    kill $( pidof com.scheduler.app.workermain )
    

    附言。您不需要将-15(或-TERM)传递给kill 命令,因为SIGTERM 是发送的默认信号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 2018-03-10
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2014-04-07
      相关资源
      最近更新 更多