我经常需要在编程期间终止一个进程。

我现在的做法是:

[~]$ ps aux | grep 'python csp_build.py'
user    5124  1.0  0.3 214588 13852 pts/4    Sl+  11:19   0:00 python csp_build.py
user    5373  0.0  0.0   8096   960 pts/6    S+   11:20   0:00 grep python csp_build.py
[~]$ kill 5124

如何自动提取进程id并在同一行中杀死它?

是这样的:

[~]$ ps aux | grep 'python csp_build.py' | kill <regex that returns the pid>

当前回答

使用pgrep -可用于许多平台:

kill -9 `pgrep -f cps_build`

pgrep -f将返回所有符合"cps_build"的pid

其他回答

我开始使用这样的东西:

kill $(pgrep 'python csp_build.py')

下面的命令将会派上用场:

Kill $(ps -elf | grep <process_regex>| awk {'print $4'})

例如, Ps -elf | grep顶部

    0 T ubuntu    6558  6535  0  80   0 -  4001 signal 11:32 pts/1    00:00:00 top
    0 S ubuntu    6562  6535  0  80   0 -  2939 pipe_w 11:33 pts/1    00:00:00 grep --color=auto top

Kill -$(ps -elf | grep top| awk {'print $4'})

    -bash: kill: (6572) - No such process
    [1]+  Killed                  top

如果进程仍然卡住,使用“-9”扩展硬杀,如下所示:

Kill -9 $(ps -elf | grep top| awk {'print $4'})

希望这对你有所帮助!

根据https://stackoverflow.com/a/3510879/15603477的回答。小的优化。

ps aux | grep 'python csp_build.py' | head -1 | tr -s ' ' | cut -d " " -f 2 | xargs kill

使用tr -s ' '将多个空白(如果有的话)压缩为一个空白。

如果你遇到不允许的行动,请跟着>> https://unix.stackexchange.com/questions/89316/how-to-kill-a-process-that-says-operation-not-permitted-when-attempted

通过关键字midori终止进程,例如:

kill sigterm $(pgrep -i midori)

试着用

ps aux | grep 'python csp_build.py' | head -1 | cut -d " " -f 2 | xargs kill