Linux:根据参数杀死进程

Mar*_*ard 69 linux kill

如何根据命令行参数终止进程? killall, pgrep, 和pkill似乎只能根据进程名称工作。

我需要这样才能区分在 Java 虚拟机中运行的许多应用程序,其中java所有应用程序的进程名称和实际应用程序名称可以通过查看命令行参数找到。

这可以手动完成ps aux | grep myapp.jar,然后从输出中手动终止 pid,但我想要一个命令来自动执行等效的操作。

Mic*_*zek 90

pgrep/pkill-f旗子。从man页面:

-f    The pattern is normally only matched against the process name.
      When -f is set, the full command line is used.
Run Code Online (Sandbox Code Playgroud)

例如:

$ sleep 30& sleep 60&
[1] 8007
[2] 8008

$ pkill -f 'sleep 30'
[1]  - terminated  sleep 30

$ pgrep sleep
8008
Run Code Online (Sandbox Code Playgroud)


Max*_*kin 5

argument下面的正则表达式替换为必须包含进程完整命令行的正则表达式:

kill `ps -eo pid,args --cols=10000 | awk '/argument/ && $1 != PROCINFO["pid"] { print $1 }'`
Run Code Online (Sandbox Code Playgroud)